Android Splash screen are normally used to show some kind of progress before the app loads completely. Some people used it show the company logo or app for few seconds. Sometime in few application there is some background activity need to be incorporated and then this splash screen play very important role. In this tutorial we are going to learn how to implement Splash screen in your android application.
1. Create a New Project in Android Studio and fill all the required details.
2. For Splash screen create a separate activity. Create anew class in your package and named it as
SplashScreen.java
3. Open your AndroidManifest.xml file and make your splash activity as default Launcher Activity
AndroidManifest.xml
4. Create a layout file for splash screen under res ⇒ layout folder. Named this layout as activity_splash.xml. This layout generally contain company logo or app logo.
activity_splash.xml
5. Add the following code in SplashScreen.java activity. In this following code a handler is used to wait for specific time and once the time is out it launch Main Activity.
SplashScreen.java
Now run the application, you will see the splash screen for 3 seconds and then your main activity will be launched.
1. Create a New Project in Android Studio and fill all the required details.
2. For Splash screen create a separate activity. Create anew class in your package and named it as
SplashScreen.java
3. Open your AndroidManifest.xml file and make your splash activity as default Launcher Activity
AndroidManifest.xml
<? xml version = "1.0" encoding = "utf-8" ?> package = "com.androidexample.splashscreen" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "8" android:targetSdkVersion = "17" /> < application android:allowBackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@style/AppTheme" > <!-- Splash screen --> < activity android:name = "com.androidexample.splashscreen.SplashScreen" android:label = "@string/app_name" android:screenOrientation = "portrait" android:theme = "@android:style/Theme.Black.NoTitleBar" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > <!-- Main activity --> < activity android:name = "com.androidexample.splashscreen.MainActivity" android:label = "@string/app_name" > </ activity > </ application > </ manifest > |
4. Create a layout file for splash screen under res ⇒ layout folder. Named this layout as activity_splash.xml. This layout generally contain company logo or app logo.
activity_splash.xml
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "@drawable/gradient_background" > < ImageView android:id = "@+id/imgLogo" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:src = "@drawable/logo" /> < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_marginBottom = "10dp" android:textSize = "12dp" android:textColor = "#454545" android:gravity = "center_horizontal" android:layout_alignParentBottom = "true" android:text = "www.androidhive.info" /> </ RelativeLayout > |
5. Add the following code in SplashScreen.java activity. In this following code a handler is used to wait for specific time and once the time is out it launch Main Activity.
SplashScreen.java
package info.androidhive.androidsplashscreentimer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class SplashScreen extends Activity { // Splash screen timer private static int SPLASH_TIME_OUT = 3000 ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_splash); new Handler().postDelayed( new Runnable() { /* * Showing splash screen with a timer. This will be useful when you * want to show case your app logo / company */ @Override public void run() { // This method will be executed once the timer is over // Start your app main activity Intent i = new Intent(SplashScreen. this , MainActivity. class ); startActivity(i); // close this activity finish(); } }, SPLASH_TIME_OUT); } } |
Now run the application, you will see the splash screen for 3 seconds and then your main activity will be launched.
No comments:
Post a Comment