Splash Screen(Using Count Down Timer)

This example will show you how can you add a splash screen to your android application. There are many ways to do that like using a handler or a timer. In our example, we going to use a count down timer.

Count-down is used when you have to perform some job based on some time and with specific intervals. The coun-down timer takes two arguments first the is specifying how long the job is being executed and the second one is for the interval.

When you create the object of count-down time we have to override some method. The first method is onTick and the other is onFinish.

1) onTick - Execute every time on the mentioned interval.
2) onFinish- Executes when the timer job is finished.

Let create and an example with it:-

Steps:-

1) Create one layout file and add the image you want to show.

2) Create one new java file.

3) Create a count down timer object to hold the image for some time and then navigate the user to the main activity.

4) Register the java file to manifest and make that file as the main activity.


Create one layout file  splash_screen.xml.


Splash_screen.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>


Create one java file splash.java.

Splash.java


public class splash extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);

new CountDownTimer(5000,5000) {

@Override
public void onTick(long millisUntilFinished) {}

@Override
public void onFinish() {
Intent maintask=new Intent(splash.this,MainActivity.class);
startActivity(maintask);
}
}.start();
}
}


Mainactivity.java    //No change


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }   
}


mainactivity.xml  //No change

<RelativeLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


Register the java file to android manifest:-

<?xml version="1.0" encoding="utf-8"?>

<manifest>

    <application>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" />            
        
        <activity android:name="com.example.splashscreenexample.splash">            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
        </activity>
    </application>
</manifest>

Output:-

1st Screen                                                            2nd Screen
                

Comments