java android studio loading screen code example
Example 1: How do I make a splash screen?
//MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//splashscreen.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class splashscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
//Splash Screen duration
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(splashscreen.this, MainActivity.class));
finish();
}
}, secondsDelayed * 3000);
}
}
//activity_main.xml
//splashscreen.xml
Source: https://github.com/Lunox-Code/SplashScreen
Example 2: splash screen android studio
///*********************declarling time here********************
private static int SPLASH_TIME_OUT = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This code is used for hide the sheek bar of application************************************
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//***********************************************************************************************
setContentView(R.layout.activity_main);
// this code is for splash screen ************************************************
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
finish();
}
},SPLASH_TIME_OUT);
}