To create Splash Screen for your Android App
- Create Handler Class
- Use PostDelayed() function
- Use Runnable (Code) & Time Delay
Steps:
A) Add Empty Activity
- Go to java -> New -> Activity -> Empty Activity
- Enter Activity Name: SplashActivity
- Hit Finish Button
B) Design Splash Screen
- Open layout -> activity_splash.xml
- Cope the Background Image you want to use
- Paste at drawables folder
- Choose a file name for your image.
- File name must be [a-z] in lowercase 0-9 digits or underscore. Capital Letter not allowed.
- Now open activity_splash.xml file
- Enter android:background="@drawable/bg_splash_screen"
- You are done
Structure of activity_splash
<?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=".SplashActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_splash_screen"
/>
C) Add Code in SplashActivity.java file
Here we use a Handler Class
PostDelayed Function to delay the display of the screen by 2500 milli second
And Runnable Method to end with.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
}
},2500);
This is how SplashActivity.java file look like:-
package xx.xxx.xxx; (It should be the name you choose while creating your android project)
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
}
},2500);
}
}
D) Load MainActivity after 2.5 Second
Use Intent like this
public void run() {
Intent intent = new Intent( SplashActivity.this,MainActivity.class);
startActivity(intent);
}
Finally our SplashActivity.java looks like this:-
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent( SplashActivity.this,MainActivity.class);
startActivity(intent);
}
},2500);
}
}
NOTE IF YOU GET RED COLOR IN Intent, hit Option + Enter button. This will import related class automatically.
Comments
Post a Comment