How To Create A Splash Activity In Android Studio

Create A Splash Activity In Android Studio


Create an eye-catching, beautiful splash activity for your android app in Android Studio. This is a simple way to create a splash activity in Android Studio easily with a minimalistic view...

This Article Includes these also:

  • Running activity in Full Screen,
  • Adding Images for ImageView,
  • Hide Toolbar/Action Bar in a specific activity,
  • Switching Activities after a specific time,
  • Example of a simple "thread" code etc

Before we get started we need These:
  • Android Studio
  • An existing App project
  • App logo (or the image you wanna show in the splash activity)

So, we are all set. Let's Get Started.....



Step 1 - Designing The Splash Activity:

Create a new Android Studio project or Open Existing One with an empty activity. Then:
  1. Open that activity (e.g. YOUR_ACTIVITY_NAME.xml) and change it to Co-Ordinator Layout/ Relative Layout
  2. Insert A Vertical Linear Layout inside the co-ordinator/relative layout and set the height and width to 'match-parent' and set the gravity to 'center'
  3. Then add an ImageView and set height and width to '120dp'
  4. Additionally, you can add a TextView or add a margin at bottom of the ImageView or linear view to set the image a little bit up from Vertically center.
So our XML file will look like this:
<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">

<ImageView
android:id="@+id/applogo"
android:layout_width="120dp"
android:layout_height="120dp"
app:srcCompat="@drawable/applogo" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">

</LinearLayout>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Indicate the location of the image logo you want to show in ImageView using 'app:srcCompat'. Simply change the "applogo" with your image file name located at the Drawable folder.

So, our design is complete. You can Clear any Doubt in the comment section.

Now we have to code...

Step 2 - Hiding The ActionBar/ToolBar And The StatusBar:

Before we jump to code our activity we need to hide the activity Toolbar/ActionBar. So, go to 'AndroidManifest' and set this attribution to your splash activity to hide the Toolbar/Action Bar:
android:theme="@style/AppTheme.NoActionBar"

Now, We need to hide the status bar and make the activity Full Screen. So, we will just go to the activity java file (e.g. YOUR_ACTIVITY_NAME.java). the import these classes along with other imports:
import android.view.Window;
import android.view.WindowManager;

Now To Hide The Status Bar, inside the 'OnCreate' event and before the 'setContentView(R.Layout.activity_ACTIVITYNAME);',
We will use the window manager to flag the activity/ start the activity in fullscreen:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

Step 3 - Running A Thread To Switch The Activity After Specific Time:

We will use a thread to redirect the user to another activity after 5 seconds/more (up to you). So, Before we Get started we need to import another class as we are going to use intent:
import android.content.Intent;

So, Now to use a thread we will use thess lines inside the 'OnCreate' event and we will name the thread 'th' :
Thread th = new Thread() {
@Override
public void run() {
try {

} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

Inside the try code ( 'try{' 'codes here' '}' ) we will use the intent and sleep code to trigger the intent after specific time.
Then We need to call the thread to run using it's name (e.g. th). SO, our final Thread Code will look like this:
Thread th = new Thread() {
@Override
public void run() {
try {
sleep(5000);
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
th.start();

Change '5000' to any time you want. (1000ms=1s)[Remember, Time must be in milliseconds Always]
So, Coding Completed. Our Complete ACTIVITY_NAME.java file will look like this:
package com.zealtyro.example;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

Thread th = new Thread() {
@Override
public void run() {
try {
sleep(5000);
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
th.start();
}
}

We Are All Set. 

Now you can add a responsive animation to the splash activity to make it more professional, to do so check this:
How To Add Responsive Animation In Splash Activity In Android Studio

Thanks For Allowing Us To Help You :)
If you are confused or want to know something then let us know in the comment box, we will reach you as soon as possible and Don't Forget To Subscribe our NewsletterYouTube Channel and Like Our Facebook Page To Keep Updated With Awesome Things. And Also to Follow us on Twitter to stay updated with the latest news & changes

Comments

You are always welcome to ask us and share your ideas
We try to bring to you the most amazing learning site for tech enthusiasts and thus work to teach you the basic and internal parts of software management, website and apps making, programming, etc. Programming and technical explications are the main points and we hereby provide you the requisite basic and a clear idea about programming and other technological interests. Our main purpose is to make you fluent in programming languages.

Subscribe

Contact Us

Send