Pages

Chapter 11: Splash Screen in Android

Splash Screen Example In Android
In this chapter will we learn how to implement the Splash Screen with a simple example in Android Studio. 

Android Splash Screen is usually used to show the blank screen or company logo when other components of the App is loading behind, Some people simply use it to show the company logo as well.
  • Create a new project as SplashScreenExample in Android Studio.
  • Create a new Activity, right click on MainActivity -> New -> Activity -> Empty Activity and name it as SplashScreen.
  • Do the following design in activity_splash_screen.xml.
File: activity_splash_screen.xml:
<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:gravity="center"
    android:padding="20dp"
    tools:context="com.example.hss_24.splashscreenexample.SplashScreen">

    <ImageView
        android:src="@drawable/simply_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
  • Now make the SplashScreen Activity as LAUNCHER activity and MainActivity as DEFAULT in AndroidMainfest.xml file
File: AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hss_24.splashscreenexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".SplashScreen">
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

  • Now do the code in SplashScreen.java.
  • Set a timer of 5 seconds and set an Intent to launch the MainActivity after 5 seconds.
File: SplashScreen.java:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashScreen extends AppCompatActivity {

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

        //    hide action bar from the activity
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        Thread timer = new Thread() {
            public void run() {
                try {
                    // setting timer
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    // launch activity 
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        timer.start();
    }
}

  • Design your main layout as you want but I have placed a Textview.
File: activity_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context="com.example.hss_24.splashscreenexample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:text="Welcome to simplifiedandroid.com" />

</RelativeLayout>
File: MainActivity.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

    }
}
  • Run the App to enjoy the output.
  • The Screen which appears while we open the App with the company logo is the Splash Screen.
Output:


Download project here 


Thank you!!!
Please like and share...



Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment