How To Make Splash Screen In Android Studio

Good UI is always important for an app. An interactive and user-friendly interface can make users satisfied. Splash Screen is great functionality for better UI. In this article, we are going to cover what it is, the basic principle of this, how you can make it.

add splash screen android studio

What is a Splash Screen?

A splash screen is an activity we can see when the app is opening or before going to another activity. This is actually a part of the user interface. This can be a medium for showing the branding, app name and logo, wishing user, marketing for other products.

Basic principles of Splash Screen

The working principle of the splash screen is simple. When the user opens an app basically it will them the main activity or a specific activity. But if you create a Splash screen then at the beginning the app will show a specific activity for a specific time then it will take the user to another activity. By using Java or Kotlin we can show activity for a specific time then we can take the user from that activity to another.

Learn how to change app name in android studio.

How to make a splash screen

We are going to use android studio for the splash screen. There is many drag and drop app makers out there where you can make this easily. But the internal error and optimization lack make that app non-standard. To make this we need an extra activity. If you already have an app then you can also add a splash screen to it. Just create a new activity and give it any name. I am going to show both Java and kotlin in this tutorial.

Create a new activity

This is going to be the splash activity where you have to add something to show to the user. Here I am giving the activity a name. In my case, I am naming it 'SplashScreen'. Select the language you want to use. I am going to show you both java and Kotlin in this article.

Create new activity in android studio

Design the splash screen activity

After creating the activity you will find an xml file and a java or kotlin file. First, you have to design the layout. I am using an image view. Make sure you added the image in the drawable folder. Here is the code for xml section of splash screen activity (activity_splash_screen.xml):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashScreen">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo"
        android:contentDescription="@string/todo" />
</androidx.constraintlayout.widget.ConstraintLayout>

Write code for splash screen

To make the splash screen work you have to write some code. Here we are going to show you both Java and Kotlin code. Here is the Java code for the SplashScreen.java file:

package example.com.app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
public class SplashScreen extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        this.getWindow().setFlags
                (WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager
                        .LayoutParams.FLAG_FULLSCREEN);
        Thread thread=new Thread(){
            @Override
            public void run()
            {
                try
                {
                    sleep(3000);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    Intent mainIntent=new Intent(SplashScreen.
                            this,MainActivity.class);

                    startActivity(mainIntent);
                }
            }
        };
        thread.start();
    }
    @Override
    protected void onPause(){
        super.onPause();
        finish();
    }
}

Here is the Kotlin Code for the SplashScreen.kt file:

package example.com.app

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler

class SplasScreen : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        supportActionBar?.hide()
        Handler().postDelayed({
val intent= Intent (this@SplashScreen, MainActivity::class.java)
        startActivity(intent)
            finish()
        }, 3000)
    }
}

Set the Splash Screen at the beginning

Go to the AndroidManifest.xml file and set the splash screen at the beginning by editing the line in the application tag. Change the activity according to the code below:

<activity android:name=".MainActivity" />
        <activity
            android:name=".SplashScreen"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

android:theme is going to change the theme for the specific activity. In this case, we are changing the theme of SplashScreen activity. We are not going to use the actionbar in this activity. <intent-filter> is going to set the launcher activity. After doing all of this just run the app and see is it working or not. Here is the full Source code file you can download.

Conclusion

Splash screen makes an app more professional. You can load data when the splash screen is running. It will save users time. You can also use lottiefiles to make your app more beautiful. Again this can be a great place where you can promote your other apps or services.

Post a Comment (0)
Previous Post Next Post