How To Print Some Text On Splash Screen While App Is Loading In Xamarin Forms
Splash Screen Tutorial With Example In Android Studio
Splash Screen is most commonly the first startup screen which appears when App is opened. In other words, it is a simple constant screen for a fixed amount of time which is used to display the company logo, name, advertising content etc.
Normally it shows when app is first time launched on android device or it may be some kind of process that is used to show screen to user just before the app loads completely.
This tutorial will help you to learn How to create Splash screen in your Android app.
Splash Screen Implementation Method In Android:
Method 1 of implementing Splash Screen:
Create a thread and set time to sleep after that redirect to main app screen.
/****** Create Thread that will sleep for 5 seconds****/ Thread background = new Thread() { public void run() { try { // Thread will sleep for 5 seconds sleep(5*1000); // After 5 seconds redirect to another intent Intent i=new Intent(getBaseContext(),FirstScreen.class); startActivity(i); //Remove activity finish(); } catch (Exception e) { } } }; // start thread background.start();
Method 2 of Implementing Splash Screen:
Set time to handler and call Handler().postDelayed, it will call run method of runnable after set time and redirect to main app screen.
Handlers are basically background threads which allows you to communicate with the UI thread (update the UI).
Handlers are subclassed from the Android Handler class and can be used either by specifying a Runnable to be executed when required by the thread, or by overriding the handleMessage() callback method within the Handler subclass which will be called when messages are sent to the handler by a thread.
There are two main uses for a Handler:
a) To schedule messages and Runnables to be executed at some point in the future
b) To enqueue an action to be performed on a different thread than your own.
new Handler().postDelayed(new Runnable() { // Using handler with postDelayed called runnable run method @Override public void run() { Intent i = new Intent(MainSplashScreen.this, FirstScreen.class); startActivity(i); // close this activity finish(); } }, 5*1000); // wait for 5 seconds
Splash Screen Image Size For Different Screen Size:
Android provides support for multiple screen sizes and densities, reflecting the many different screen configurations that a device may have. You can prefer below sizes to support Splash Screen on different size smartphones.
Android divides the range of actual screen sizes and densities into:
A set of four generalizedsizes: small, normal, large, and xlarge
A set of six generalizeddensities:
- ldpi (low) ~120dpi (240x360px)
- mdpi (medium) ~160dpi (320x480px )
- hdpi (high) ~240dpi (480x720px)
- xhdpi (extra-high) ~320dpi (640x960px)
- xxhdpi (extra-extra-high) ~480dpi (960x1440px)
- xxxhdpi (extra-extra-extra-high) ~640dpi (1280x1920px)
Splash Screen Example in Android Studio
With the help of this tutorial we will cover implementation of splash screen in two scenarios. First will show splash screen using Handler and second we will not create a layout file for splash screen activity. Instead, specify activity's theme background as splash screen layout.
In the first below example we will see the use of Splash Screen using Handler class.
Below you can download code, see final output and step by step explanation of example:
Download Code
Step 1 : Create a new project and name it Splashscreen
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we simply added a code to display layout after Splash screen.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="abhiandroid.com.splashscreen.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World by AbhiAndroid!" android:textSize="20sp" android:layout_centerInParent="true"/> </RelativeLayout>
Step 3: Create a new XML file splashfile.xml for Splash screen and paste the following code in it.
This layout contains your app logo or other product logo that you want to show on splash screen.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@color/splashBackground"> <ImageView android:id="@+id/logo_id" android:layout_width="250dp" android:layout_height="250dp" android:layout_centerInParent="true" android:src="@drawable/abhiandroid"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/logo_id" android:layout_centerHorizontal="true" android:text="Splash Screen" android:textSize="30dp" android:textColor="@color/blue"/> </RelativeLayout>
Step 4: Now open app -> java -> package -> MainActivity.java and add the below code.
package abhiandroid.com.splashscreen; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 5: For Splash Screen we will create a separate splash activity. Create a new class in your java package and name it as SplashActivity.java.
Step 6: Add this code in SplashActivity.java activity. In this code handler is used to hold the screen for specific time and once the handler is out, our main Activity will be launched. We are going to hold the Splash screen for three second's. We will define the seconds in millisecond's after Post Delayed(){} method.
1 second =1000 milliseconds.
Post Delayed method will delay the time for 3 seconds. After the delay time is complete, then your main activity will be launched.
SplashActivity.java
package abhiandroid.com.splashscreen; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; /** * Created by AbhiAndroid */ public class SplashActivity extends Activity { Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashfile); handler=new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent intent=new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); finish(); } },3000); } }
Step 7: Open AndroidManifest.xml file and make your splashactivity.java class as Launcher activity and mention the Main Activity as another activity.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="abhiandroid.com.splashscreen"> <application android:allowBackup="true" android:icon="@drawable/abhiandroid" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="abhiandroid.com.splashscreen.SplashActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="abhiandroid.com.splashscreen.MainActivity"/> </application> </manifest>
Output:
Now run the App and you will see Splash screen before the main layout loads.
Splash Screen Example 2 In Android Studio:
The second way is little different to implement Splash screen. In this example we are going to specify the Splash screen background as an activity's theme background.
Below you can download code, see final output and step by step explanation of example:
Download Code
Step 1:Create a new project and name it Splashscreen2
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step the code to display layout after Splash screen is added.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" android:layout_gravity="center" tools:context="abhiandroid.com.splashscreen2.SecondActivity"> <TextView android:id="@+id/hello_id" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello my friend " android:textSize="20sp"/> </RelativeLayout>
Step 3: Create a new xml layout in res ⇒ drawable ⇒ splash_screenbackground.xml
In this example we will not create a layout file for Splash screen activity. Instead we will use activity theme background for Splash screen layout.
Here, we use a layer list to show the image in the center of splash screen. It is necessary to use a bitmapped image to display the image. ( image should be PNG or JPG) and splash_background_color as an background color.
The following is an example code of a drawable resource using layer-list.
Add below code in res ⇒ drawable.xml ⇒ splash_screenbackground.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <color android:color="@color/splash_background_color"/> </item> <item> <bitmap android:src="@drawable/mx" android:tileMode="disabled" android:gravity="center"/> </item> </layer-list>
Step 4: Now open res ⇒ values ⇒ colors.xml
Make sure to add below code in colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="splash_background_color">#5456e1</color> </resources>
Step 5: Add the below code inres ⇒values ⇒ styles.xml
Now we create a theme for the splash screen activity. We create a custom theme for the splash screen Activity, and add to the file values/styles.xml
res ⇒values ⇒ styles:
SplashTheme – When we declare the window background, it will removes the title bar from the window, and show us the full-screen. The file is shown here with a style named SplashTheme.
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="SplashTheme" parent ="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screenbackground</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style> </resources>
Step 6: Add the below code in MainActivity.java and Create a new SecondActivity.java for splash screen
Here we will not define the setcontentview() for the Splash screen activity because we directly apply the Splash Theme on it. Now we just have to launch the Activity(Second Activity) and then finish the activity by calling finish() method.
MainActivity.java
package abhiandroid.com.splashscreen2; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startActivity(new Intent(MainActivity.this,SecondActivity.class)); // close splash activity finish(); } }
SecondActivity.java
package abhiandroid.com.splashscreen2; import android.app.Activity; import android.os.Bundle; /** * Created by AbhiAndroid */ public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 7: Theme must be assigned to Main Activity in Android Manifest
Like – (android:name=".MainActivity"android:theme="@style/SplashTheme"> )
Here is the full code of AndroidManifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="abhiandroid.com.splashscreen2"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="abhiandroid.com.splashscreen2.MainActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="abhiandroid.com.splashscreen2.SecondActivity"/> </application> </manifest>
Output:
Now run the App and you will Splashscreen launch before the main activity.
How To Print Some Text On Splash Screen While App Is Loading In Xamarin Forms
Source: https://abhiandroid.com/programming/splashscreen
Posted by: joneslieve1996.blogspot.com
0 Response to "How To Print Some Text On Splash Screen While App Is Loading In Xamarin Forms"
Post a Comment