How to create a splash screen in android application

Srinivas Gunti
2 min readNov 24, 2019

--

Photo by Jonathan Kemper on Unsplash

activity_splash.XML

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:srcCompat="@drawable/splash_screen_background" />

<ImageView
android:layout_width="322dp"
android:layout_height="307dp"
android:layout_centerInParent="true"
app:srcCompat="@drawable/logo"
android:id="@+id/app_logo"/>

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/img"
android:progressDrawable="@color/colorPrimary" />

</RelativeLayout>

ActivitySplash.java

package com.splashApplication;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import java.util.HashMap;

public class ActivitySplash extends AppCompatActivity {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash);

if(!isNetworkAvailable(getApplicationContext())){
Toast.makeText(getApplicationContext(), "No internet connection is available",Toast.LENGTH_SHORT).show();
}

/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent i = new Intent(ActivitySplash.this, ActivityMain.class);
ActivitySplash.this.startActivity(i);
}

}, SPLASH_DISPLAY_LENGTH);
}

public boolean isNetworkAvailable(Context ctx) {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
if (activeNetworkInfo.isConnected() || activeNetworkInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
} else {
return false;
}
} catch (Exception e){
return false;
}
}

}

AndroidManifest.xml

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

<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"></activity>
<activity android:name=".ActivitySplash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

I hope this tutorial helps you. Happy coding:)

--

--

Srinivas Gunti
Srinivas Gunti

Written by Srinivas Gunti

Software Engineer | Amateur Writer | Traveller | Tech Enthusiast

No responses yet