Splash Screen in Android using JAVA
Hello friends,we are now going to show how to show a splash screen on the starting of your app. Firstly, we start by using the simple screen to show how it works. After that we will using animations in splash screen. If you are new to Android development follow all the steps.
Now, go to Android Studio and make a new Project.
In next Window, select Empty View Activity and click on Next.
Now, change the name to Splash Screen as shown below and select the language to JAVA.
Now, add the following code in MainActivity.java
MainActivity.java :
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
public static final int SPLASH_SCREEN_TIME_OUT = 3000;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayourParams.FLAG_FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.id.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_SCREEN_TIME_OUT);
}
}
Now, go to Projects Tab, Right click on com.example.spalshscreen and go to NEW -> ACTIVITY -> EMPTY VIEWS ACTIVITY and create a new activity named as NextActivity as shown below.
Now, run your app. For first three seconds, MainActivity will be shown and after that switched to NextActivity.
Go to activity_main.xml and write the following code in it. Anything you want to do here will be shown on splash screen.
activity_main.xml :
<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:background="@color/design_default_color_primary"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>