Saturday 20 May 2017

How to create Splash screen in android

Android splash screen are normally used to show user some kind of progress before the app loads completely or show company logo.



splash screen demo
Splash Screen



First create android project in Android studio and give activity name as Splash.

Now Paste following code in your activity_splash.xml file.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context="com.app.splashscreen.SplashActivity">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_android"/>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="38dp" />
    
</RelativeLayout>


Now Paste following code in your Splash.java file.


import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashActivity extends AppCompatActivity {

    int SPLASH_TIME_OUT = 1500;

    @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(getApplicationContext(),HomeActivity.class));
                finish();
            }
        },SPLASH_TIME_OUT);
    }
}

Create one another activity named HomeActivity to open after Splash scree.

No comments:

Post a Comment