We are here to show how to add a very simple splash screen in Android Application .
First, create a java class and name it as SplashScreen.java .
Here’s the SplashScreen.java file.

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;

/**
 * Created by nym
 */
public class SplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent intent = new Intent(SplashScreen.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        };
        timerThread.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

place your image (800*1280) in res/drawable naming it as splash.jpg/png

After that create a XML file under layout directory and name is as splash.xml . Write the given XML code in there.

res/layout/splash.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash"
    android:orientation="vertical">


Changes in AndroidManifest.xml 
There are some changes in the manifest file.

Here we are adding the SplashScreen.java as an acitivity and the activity filters are set to action MAIN and in LAUNCHER category. We have set these because this activity will now launch before the MAINACTIVITY file opens.
Don’t forget to change the package name if you are using the project directly by copying.


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.snipernym.splashscreen">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen"
            android:label="@string/app_name" >
            
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            
        
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            
                <action android:name="com.snipernym.splashscreen.MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            

        
    


Hope you have successfully added Splash Screen in your android App 🙂

Advertisements