How to create Splash screen in your Android app

0
229

Welcome back to shortlearner.com, in our previous post we learn the complete installation process of android studio.
today we learn what is splash activity and how to create it.
first of all we should know about what is splash activity, so basically splash screen is the user’s first experience of your application.

It normally used to display some kind of progress before the application setup completely.it is a simple constant screen for a fixed amount of
time which is used to display the company logo, name, advertising content etc.This tutorial will help you to learn How to create Splash screen in your Android app.

Implementation

To implement above approach,
first create an XML layout , Open res -> layout -> activity_main.xml and add following code:
In this step we simply added our code to display layout after Splash screen run.

activity_main.xml

<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="welcome to Shortlearner!"
        android:textSize="20sp"
        android:layout_centerInParent="true"/>
</RelativeLayout>

Now create splash activity design in xml

activity_splash.xml

<?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/logo"/>

    <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>

Now open app -> java -> package -> MainActivity.java and add the below code.

package shortlearner.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);


    }
}

splashactivity.java

package shortlearner.com.splashscreen;

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

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);

    }
}

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shortlearner.com.splashscreen">

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="shortlearner.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="shortlearner.com.splashscreen.MainActivity"/>
</application>

</manifest>