How to create Splash Screen in Android Studio
With the help of this blog we will cover implementation of splash screen in two scenarios. First will show splash screen using Handler & second we will not create a layout file for splash screen activity. Instead, specify activity’s theme background as splash screen layout.
In the first below example we will see the use of Splash Screen using Handler class.
For More Detailed Information: Click Here
How to Create a code Step by Step Guidance
STEP 1: Create a new project and the project name is Splashscreen1
STEP 2: Open res -> layout -> activity_main.xml (or) main.xml & add following code
In this step we simply added a code to display layout after Splash screen.
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/activity_main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello World”
android:textSize=”20sp”
android:layout_centerInParent=”true”/>
</RelativeLayout>
STEP 3: Create a new xml file splashfile.xml for Splash screen and paste the following code in it.
This layout contains your app logo or other product logo that you want to show on splash screen.
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”
android:gravity=”center”
android:background=”@color/splashBackground”
tools:context=”.MainActivity”>
<ImageView
android:id=”@+id/logo_id”
android:layout_width=”250dp”
android:layout_height=”250dp”
android:layout_centerInParent=”true”
android:src=”@drawable/logo” />
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/logo_id”
android:layout_centerHorizontal=”true”
android:text=”Splash screen”
android:textSize=”30dp”
android:textColor=”@color/blue”/>
</RelativeLayout>
STEP 4: Now open app -> java -> package -> MainActivity.java and add the below code
package com.example.splashscreen1;
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 5: For Splash Screen we will create a separate splash activity. Create a new class in your java package and name it as SplashActivity.java.
STEP 6: Add this code in SplashActivity.java activity. In this code handler is used to hold the screen for specific time and once the handler is out, our main Activity will be launched. We are going to hold the Splash screen for three second’s. We will define the seconds in millisecond’s after Post Delayed(){} method.
1 second =1000 milliseconds.
Post Delayed method will delay the time for 3 seconds. After the delay time is complete, then your main activity will be displayed.
SplashActivity.java
package com.example.splashscreen1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class splash extends AppCompatActivity {
Handler handler;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(splash.this,MainActivity.class);
startActivity(intent);
finish();
}
},3000);
}
}
STEP 7: Open AndroidManifest.xml file and make your splashactivity.java class as Launcher activity and mention the Main Activity as another activity.
AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.splashscreen1″>
<application
android:allowBackup=”true”
android:icon=”@drawable/logo”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.splash”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.MainActivity”/>
</application>
</manifest>