How to Make Splash Screen Without Blank Screen Before Launching an Activity in Android Studio

Splash Screen in Android is the first screen of an app which appears for a fixed amount of time before the app is opened. We use this screen to display some kind of progress before the app is completely loaded as well as to display some basic info like app name, app logo etc.

In this post, we learn how to create a splash screen and how to remove blank screen which appears before the launching of the splash screen.

Problem of Blank Screen

Most of the developer use an activity to create a splash screen, which leads to the problem of the blank screen appears before launching of the splash screen.

Solution of Blank Screen

The main reason of blank screen is that your layout file is visible only after the app is fully started. You can resolve this problem by specifying the background of your splash screen as the theme of the activity background. You have to just add a piece of code in your style resource then configure SplashTheme as the theme of your splash activity in your AndroidManifest.xml.

A piece of code is given below to add in your style resource:

<style name=”SplashTheme” parent=”AppTheme”>
    <item name=”android:windowBackground”>@drawable/ss_bg</item>
</style>

Demo

Create New Project

Before Implementing the following code first create a new project in Android Studio, go to File  => then go to New => then select New Project option. In Add an activity option select default activity(Blank Activity) and proceed.

Structure Of Code

Code

Code for styles.xml file.

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/ss_bg</item>
</style>

</resources>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asus.splashscreen">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Splash"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
</application>

</manifest>

 

Code for MainActivity.java file.

package com.example.asus.splashscreen;

import android.support.v7.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);
}
}

Code for activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#80abc8"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="WELCOME"
android:textColor="#000"
android:textSize="20dp"
android:textAlignment="center"
android:gravity="center"/>

</android.support.constraint.ConstraintLayout>

 

Code for Splash.java file.

package com.example.asus.splashscreen;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Splash extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
},1000);

}
}

 

Code for activity_splash.xml file.

<?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"
tools:context=".Splash">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg_tsp"
android:scaleType="fitXY"/>

</RelativeLayout>

 

Screenshot

 

 

 

How to Make Splash Screen Without Blank Screen Before Launching an Activity in Android Studio
Tagged on:

2 thoughts on “How to Make Splash Screen Without Blank Screen Before Launching an Activity in Android Studio

  • July 10, 2018 at 4:54 pm
    Permalink

    your code is working correctly for me. Thanks.
    Looking forward to see more post on it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *