Search code examples
androidxmlandroid-studioandroid-layoutsplash-screen

animated splash screen doesn't appear


In this project I tried to make an animated splash screen which will appear before going to the main activity, but after I execute, the splash screen does not appear but instead goes directly to the main activity. how to fix this?

this is my splash activity class

    private ImageView container;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash)
        container = findViewById(R.id.iv_icons);
        container.setBackgroundResource(R.drawable.mysplash_animation);

        animationDrawable = (AnimationDrawable) container.getBackground();

    }

    @Override
    protected void onResume() {
        super.onResume();

        animationDrawable.start();

        checkAnimationStatus(50, animationDrawable);
    }

    private void checkAnimationStatus(final int time, final AnimationDrawable animationDrawable) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (animationDrawable.getCurrent() != animationDrawable.getFrame(animationDrawable.getNumberOfFrames() - 1))
                    checkAnimationStatus(time, animationDrawable);
                else finish();
            }
        }, time);
    }
}

and this is my manifest.xml

 <application
        android:allowBackup="true"
        android:exported="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".SplashActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Solution

  • You have to change this in your manifest(you have to give launcher activity to splashActivity)

        <application
        android:allowBackup="true"
        android:exported="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".SplashActivity">
         // below code you have to add //
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
          //Remove from here//
        </activity>
    </application>