Animated Splash Screen In Android

Splash Screen In Android

In this tutorial we will learn how to implement Splash Screen in Android with an example.

Android Splash Screen is used to show the user before the App loads completely, some people use it to show their App company logo for the couple of minute.
Splash Screen is an Activity that will show for the set time if the set time ends it will re direct to the Main screen.

Example:
  • Create a new project.
  • Do nothing in activity_main.xml and MainActivity.java.
  • Create a new Activity as SplashScreen.
  • In splash.xml do the following.
File: splash.xml:


<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="@drawable/bg"    
android:gravity="center"    
android:orientation="vertical">

<TextView        
android:id="@+id/text"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:gravity="center_horizontal"        
android:text="AndroidLovers"        
android:textSize="40sp"        
android:textStyle="bold" />

</LinearLayout>


  • Create a new Directory for storing Animation files as anim, right click on res -> New -> Directory and name it as anim.
  • Create a Animation xml, right click on anim -> New -> Animation resource file and name it as fade.
  • Do the following in fade.xml.
File: fade.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">

<alpha        
android:duration="2000"        
android:fromAlpha="0"        
android:toAlpha="1">
    </alpha>

<alpha        
android:duration="2000"        
android:fromAlpha="1"        
android:startOffset="2000"        
android:toAlpha="0">       
    </alpha>

</set>

</set>

  • Now lets go for coding, do the following in SlpashScreen.java.
File: SplashScreen.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class SplashScreen extends Activity {
    TextView textView;

    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        textView = (TextView) findViewById(R.id.text);
        Thread timer = new Thread() {
            public void run() {
                try {
                    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
                    textView.startAnimation(animation);
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);
                }
            }
        };
        timer.start();
    }

    @Override    protected void onPause() {
        super.onPause();
        finish();

    }

}


  • We have to set SplashScreen as launcher and MainActivity as default in Manifest, so lets go to Manifest and change the following.
File: Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.hss_24.splashscreen">

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

  • Run the Application.
Thank you!!!
Please like and share...
LeEco Day on Flipkart

Comments