Lord Goderick
Lord Goderick

Reputation: 985

Android: Splash Screen Implementation Not Working

I am trying to implement a splash screen before the MainActivity but cannot get it working. I followed the instructions found here: https://www.bignerdranch.com/blog/splash-screens-the-right-way/. Currently nothing appears but a black delayed screen. Please tell me where I went wrong. Thanks.

Splash.java

public class Splash extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, WelcomeActivity.class);
        startActivity(intent);
        finish();
    }
}

background_splash.xml

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <item
            android:drawable="@color/colorAccent"/>
        <item>
            <bitmap
                android:gravity="center"
                android:src="@drawable/splash"/>
        </item>

styles.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

Manifest

<activity android:name=".Splash"
            android:theme="@style/SplashTheme">

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

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

        </activity>

Upvotes: 1

Views: 780

Answers (2)

Abhi Muktheeswarar
Abhi Muktheeswarar

Reputation: 394

Simple, Uninstall & install the app again, you will get the splash screen. The problem is due to the instant run in Android Studio

To test, give a delay before starting the new activity:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    startActivity();
  }
}, 100);

Upvotes: 2

Vasileios Pallas
Vasileios Pallas

Reputation: 4867

Below super.onCreate(savedInstanceState); add setContentView(R.layout.layout_name.xml);

Upvotes: 0

Related Questions