Create Splash Screen In Android
Creating splash is a three step simple procedure
- Create XML splash.xml and copy this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_gravity="center">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/splash"
android:scaleType="centerInside"
android:gravity="center"/>
</LinearLayout>
2. Create JAVA file splash.java and copy this code
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Window;
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
protected void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
@Override
protected void onResume()
{
super.onResume();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
// Obtain the sharedPreference, default to true if not available
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled)
{
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
//Finish the splash activity so it can't be returned to.
SplashActivity.this.finish();
// Create an Intent that will start the main activity.
Intent mainIntent = new Intent(splash.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
}
}, SPLASH_DISPLAY_LENGTH);
}
else
{
// if the splash is not enabled, then finish the activity immediately and go to main.
finish();
Intent mainIntent = new Intent(splash.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
}
}
}
3. Now third and final step open AndroidManifest.xml file and copy this code under application tag
<activity
android:name=".splash"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And your work is done, splash is ready to explode....
Please leave comment if any problem occurs...............................