Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaSplash Screen in Android

Splash Screen in Android

A splash screen is mostly the first screen of the app when it is opened. It is a constant screen that appears for a specific amount of time and generally shows for the first time when the app is launched. Splash screen is used to display some basic introductory information such as the company logo, content, etc just before the app loads completely. In this article, we will look at How to Make a Splash Screen in Android. A sample video is given below to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

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.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple_200"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--on below line we are adding an image view-->
    <ImageView
        android:id="@+id/idIVLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="25dp"
        android:src="@drawable/gfglogo"
        android:tint="@color/white" />
  
    <!--on below line we are creating progress bar-->
    <ProgressBar
        android:id="@+id/idPBLoading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/idIVLogo"
        android:layout_centerInParent="true"
        android:indeterminateTint="@color/white" />
  
</RelativeLayout>


Step 3: Creating a new activity

Navigate to app>java>your app’s package name> Right-click on it>New>Activity>Empty Activity and specify name as MainActivity2. 

Step 4: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
  
        // on below line we are configuring 
        // our window to full screen
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        setContentView(R.layout.activity_main)
          
        // on below line we are calling
        // handler to run a task
        // for specific time interval
        Handler().postDelayed({
            // on below line we are 
            // creating a new intent
            val i = Intent(
                this@MainActivity,
                MainActivity2::class.java
            )
            // on below line we are
            // starting a new activity.
            startActivity(i)
              
            // on the below line we are finishing
            // our current activity.
            finish()
        }, 2000)
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          
        // on below line we are configuring our window to full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
        setContentView(R.layout.activity_main);
  
        // on below line we are calling handler to run a task
        // for specific time interval
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // on below line we are 
                // creating a new intent
                Intent i = new Intent(MainActivity.this, MainActivity2.class);
                  
                // on below line we are 
                // starting a new activity.
                startActivity(i);
                  
                // on the below line we are finishing 
                // our current activity.
                finish();
            }
        }, 2000);
  
    }
}


Step 5: Working with the activity_main2.xml file

Navigate to app>res>layout>activity_main2.xml and add the below code to it. Comments are added in the code to get to know in detail. 

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"
    tools:context=".MainActivity2">
  
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Welcome to Geeks for Geeks"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
</RelativeLayout>


Step 6: Adding a style for MainActivity in the AndroidManifest.xml file

Navigate to app>AndroidManifest.xml file and add the below code to it for MainActivity in the application section. 

XML




<!--on below line we are adding a style for MainActivity-->
<activity
      android:name=".MainActivity"
      android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>


Now run your application to see the output of it. 

Output:

RELATED ARTICLES

Most Popular

Recent Comments