-
Create new Android Project call Android Splash Screen and put your launch image at res/drawable folder.
-
Also create three xml file one for splash screen give name splashscreen.xml in res/layout folder and create one new folder in res folder and give name anim and create two xml file called fade_in.xml and fade_out.xml for animation.
-
Modify Animations xml file as per below code.
fade_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="4000"/> //Milliseconds
</set>
fade_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="4000"/> //Miliseconds
</set>
-
Now modify your main.xml and splashscreen.xml file as per below code
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:gravity="center"
>
<TextView
android:id="@+id/tv1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:textSize="20sp"
android:text="@string/main_screen"
android:textColor="#000000" />
</RelativeLayout>
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:background="@drawable/splash_screen"
android:layout_height="fill_parent"
android:orientation="vertical" >
</RelativeLayout>
-
Now Create one SplashScreen.java file in src folder and modify as below code
SplashScreen.java
package com.idroid.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
private static final int TIME = 4 * 1000;// 4 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(intent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, TIME);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, TIME);
}
@Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}
-
Now build and run your project on Simulator you can see the splash screen for 4 seconds.