Search code examples
javaandroidiconsgoogle-play-servicessplash-screen

How to add splash screen


i'm a newwbie in java, and i want to add splash screen to a code. so i have created a layout XML named splash.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash" >

</RelativeLayout>

and i have created a java classe named splash.java :

package com.test.test;


import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import android.view.Window;

import com.test.test.R;


public class Splash extends Activity {

private boolean backbtnPress;
private static final int SPLASH_DURATION = 3000;
private Handler myHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();

    setContentView(R.layout.splash);

    myHandler = new Handler();
    myHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
            finish();

            if(!backbtnPress)
            {
                Intent intent = new Intent(Splash.this,MainActivity.class);
                Splash.this.startActivity(intent);
            }

        }
    }, SPLASH_DURATION);

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub

    backbtnPress = true;
    super.onBackPressed();
}
}

but it does not work, when i run the app i find two app icons on device.

can you help me please

Thank you in advance!


Solution

  • <activity
        android:name=".Splash"
     >     
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />>     
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    < /activity>