user820913 user820913 - 3 months ago 19
Android Question

Showing logo 3 seconds before loading mainActivity

I want to make a logo (an own activity)show in an own activity 3 seconds before the main activity loads, when starting my android app. Which is the simplest approach for doing this?

I have searched through this forum, I could only find one answered question around this topic, but unhappily it was unusefull for me.

Answer

I think what you're referring is how to implement a Splash screen,

Create a new empty activity, I'll call it Splash for this example;

public class SplashScreen extends Activity {

    // Sets splash screen time in miliseconds
    private static int SPLASH_TIME = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {

                // run() method will be executed when 3 seconds have passed

                //Time to start MainActivity
                Intent intent = new Intent(Splash.this, MainActivity.class);
                startActivity(intent );

                finish();
            }
        }, SPLASH_TIME);
    }

}

Make sure you've set Splash activity as the launcher activity in your Manifest file :

 <activity
     android:name=".Splash"
     android:theme="@android:style/Theme.NoTitleBar">
       <intent-filter>

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

       </intent-filter>
</activity>
Comments