Simple Splash Screen in android example

Most of the developers need to add splash screen on their application, but some of them really don't know how to do that with small effort.

Here is the new example where you can add the splash screen in your application.

First you need to create an activity with the xml layout file.

Paste the below code to your SpalshActivity.java class:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.apppackage.app;

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

public class SplashActivity extends Activity {

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

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
			startActivity(new Intent(SplashActivity.this, NextActivity.class));
            finish();
            }
        }, 3000); //Delay of splash
    }
}


Next code is for your activity_splash.xml file paste the below code to your layout file.





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="48dp"
        android:gravity="center"
        android:text="App Name"
        android:textAllCaps="false"
        android:textSize="20dp" />

</RelativeLayout>

Finally you need to make your splash screen activity a main launcher, for that you need to remove below code from main activity and add this to your SplashActivity in manifest file.



1
2
3
4
5
6
7
	<activity android:name=".SplashActivity">
		<intent-filter>
			<action android:name="android.intent.action.MAIN" />

			<category android:name="android.intent.category.LAUNCHER" />
		</intent-filter>
	</activity>

That's it, by doing so you can add a simple splash activity to your android application.


 
Happy Coding!
Thanks.

Comments