Quantcast
Channel: The Crazy Programmer
Viewing all articles
Browse latest Browse all 761

Android Splash Screen Example

$
0
0

In this tutorial you will get android splash screen example.

Splash screen is a simple screen or activity used to display some progress when application is launched. Some developers use splash screen to just show company logo or to engage user while some important data in loaded in background. After few seconds or minutes splash screen is stopped and any other activity is started.

Below I have given an example of splash screen of Facebook app. This screen appears for few seconds when app is launched.

facebook splash screen

Android Splash Screen Example

Create new project with a blank activity with name Splash. Use package name as com.splash. Now create another blank activity with name MainActivity, it is the activity that will open after splash screen.

Note: Make sure Splash is launcher activity.

Copy any image with name logo in res/drawable folder. We will use this logo to show on splash screen.

Now add following codes in respective files.

Splash.java

package com.splash;

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

public class Splash extends Activity {

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

        //creating thread that will sleep for 10 seconds
        Thread t=new Thread() {
            public void run() {

                try {
                        //sleep thread for 10 seconds, time in milliseconds
                        sleep(10000);

                        //start new activity
                        Intent i=new Intent(Splash.this,MainActivity.class);
                        startActivity(i);

                        //destroying Splash activity
                        finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        //start thread
        t.start();
    }
}

 

activity_splash.xml

<LinearLayout 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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" tools:context="com.splash.Splash">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_gravity="center"/>
</LinearLayout>

 

MainActivity.java

package com.splash;

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

public class MainActivity extends Activity {

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

 

activity_main.xml

<LinearLayout 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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" tools:context=".MainActivity">

    <TextView android:text="Hello World" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

 

Android Splash Screen Example
I have used Thread class and sleep() method to keep splash screen for 10 seconds. You can change the time according to you. After that Intent class is used to open MainActivity.

If you are facing any problem related to above android splash screen example then feel free to ask it by commenting below.

Happy Coding!! :) :)

The post Android Splash Screen Example appeared first on The Crazy Programmer.


Viewing all articles
Browse latest Browse all 761

Trending Articles