We are here to show how to add animated splash screen in Android App .

First, create a java class and name is as SplashScreen.java .

Here’s the SplashScreen.java file.

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by nym
 */

public class SplashScreen extends Activity {
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }
    /** Called when the activity is first created. */
    Thread splashTread;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        StartAnimations();
    }
    private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
        l.clearAnimation();
        l.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.translate);
        anim.reset();
        ImageView iv = (ImageView) findViewById(R.id.splash);
        iv.clearAnimation();
        iv.startAnimation(anim);

        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // Splash screen pause time
                    while (waited < 7000) {
                        sleep(100);
                        waited += 100;
                    }
                    Intent intent = new Intent(SplashScreen.this,
                            MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                    SplashScreen.this.finish();
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    SplashScreen.this.finish();
                }

            }
        };
        splashTread.start();

    }

res/anime/alpha.xml


After that create a directory under folder “res” . Name that directory as anime and create a XML file under that directory and name is as alpha.xml . Write the given XML code in there.



<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="3000" />

res/anime/translate.xml


<set
    xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="0%"
        android:toXDelta="0%"
        android:fromYDelta="200%"
        android:toYDelta="0%"
        android:duration="3000"
        android:zAdjustment="bottom" />


place your image (800*1280) in res/drawable naming it as splash.jpg/png

After that create a XML file under layout directory and name is as splash.xml .
Write the given XML code in there.

res/layout/splash.xml



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#242729"
    android:layout_gravity="center"
    android:id="@+id/lin_lay"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/splash"
        android:background="@drawable/splash" />



Changes in AndroidManifest.xml

There are some changes in the manifest file.

Here we are adding the SplashScreen.java as an acitivity and the activity filters are set to action MAIN and in LAUNCHER category. We have set these because this activity will now launch before the MAINACTIVITY file opens.

Don’t forget to change the package name if you are using the project directly by copying.


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.snipernym.simplesplashscreen">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen"
            android:label="@string/app_name" >
            
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            
        
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            
                <action android:name="com.snipernym.splashscreen.MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            

        
    


Hope you have successfully integreated beautiful animated Splash Screen in your android App 🙂

Advertisements