Thursday, March 1, 2012

Splash Screen in Android

If you wanted splash screen in Android apps, Splash screen will come first for few second then from splash screen main screen will lunch.
R.layout.splash -> this is .xml layout, it will show how you wanted to show your splash screen.
startActivity(new Intent(SplashScreen.this, MainScreen.class)) - > Mainscreen will be your next screen you wanted to come automatically after splash screen.
CountDownTimer timer = new CountDownTimer(1000, 1000)  -> splash screen will visible for 1 sec.




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

public class SplashScreen extends Activity {
    /** Called when the activity is first created. */
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        CountDownTimer timer = new CountDownTimer(1000, 1000) {
            @Override
            public void onFinish() {
                startActivity(new Intent(SplashScreen.this, MainScreen.class));
            }
            @Override
            public void onTick(long arg0) {
                // TODO Auto-generated method stub
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}