A splash screen is mostly the first screen of the app when it is opened. It is a constant screen which appears for a specific amount of time, generally shows for the first time when the app is launched. The Splash screen is used to display some basic introductory information such as the company logo, content, etc just before the app loads completely.
Creating Splash screen using handler in Android
Here we created two activities MainActivity showing the Splash Screen and SecondActivity in order to switch from MainActivity to SecondActivity. The main program is written in MainActivity, you can change activities as per your need.
- To remove the ActionBar, you need to make following changes in your styles.xml file.
style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" ...
- Use colors which is suitable for your application.
- No need to make any changes in your manifest file.
MainActivity.java
Java
package com.example.hp.splashscreen; import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.os.Handler; import android.view.WindowManager; public class MainActivity extends AppCompatActivity { private static final int SPLASH_SCREEN_TIME_OUT = 2000 ; // After completion of 2000 ms, the next activity will get started. @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // This method is used so that your splash activity can cover the entire screen. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); // this will bind your MainActivity.class file with activity_main. new Handler().postDelayed( new Runnable() { @Override public void run() { // Intent is used to switch from one activity to another. Intent i = new Intent(MainActivity. this , SecondActivity. class ); startActivity(i); // invoke the SecondActivity. finish(); // the current activity will get finished. } }, SPLASH_SCREEN_TIME_OUT); } } |