Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaDynamic Splash Screen in Android

Dynamic Splash Screen in Android

A Dynamic Splash screen is a more personalized image or graphic that is displayed when an application is loaded or launched. Dynamic Splash Screen consists of animation or moving graphics. It appears for a fraction of a second. It creates a sense of anticipation or excitement for the user and helps organizations with branding and establishing identity.

Note: For creating a dynamic splash screen, use Lottie animations

Lottie Animation 

Lottie is an open-source animation tool developed by Airbnb that allows developers or designers to export animations and render them in mobile applications or web applications.

Step-by-Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Adding Lottie dependency in build.gradle(Module:app)

implementation 'com.airbnb.android:lottie:5.2.0'

Step 3: Adding Lottie animation in the raw resource directory

Navigate to the app > res > raw > android.json

Note: android.json is just an example of Lottie animation, you can change it as per your needs

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:gravity="center"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#GeeksForGeeks"
        android:textSize="20sp"
        android:textColor="#2F8C46"
        android:textStyle="bold|italic" />
  
</LinearLayout>


Step 5: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




package com.anas.gfgdynamicsplashscreen;
  
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


Step 6: Working with the activity_splash.xml file

Navigate to the app > res > layout > activity_splash.xml and add the below code to that file. Below is the code for the activity_splash.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context=".SplashActivity">
  
    <com.airbnb.lottie.LottieAnimationView
        android:layout_width="280dp"
        android:layout_height="280dp"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        android:layout_centerInParent="true"
        app:lottie_rawRes="@raw/android">
    </com.airbnb.lottie.LottieAnimationView>
    
</RelativeLayout>


Step 7: Working with SplashActivity.java

Java




package com.anas.gfgdynamicsplashscreen;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
  
public class SplashActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
            }
        },5000);
    }
  
    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
  
}


Step 8: Working with the themes.xml file

Navigate to the app > res > themes > themes.xml and add the below code to that file. Below is the code for the themes.xml file

XML




<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.GfgDynamicSplashScreen" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">#1B5028</item>
        <!-- Customize your theme here. -->
    </style>
    <style name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
  
    </style>
</resources>


Step 9: Working with AndroidManifest.xml

XML




<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools">
  
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.GfgDynamicSplashScreen"
        tools:targetApi="31">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/Theme.SplashScreen"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="false">
        </activity>
    </application>
  
</manifest>


Output:

RELATED ARTICLES

Most Popular

Recent Comments