Viktor Mićanović
Viktor Mićanović

Reputation: 13

Splash Screen - white

For the splash screen I'm using:

setContentView(R.layout.activity_home);
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(),MainActivity.class));
    }
}, 3500);

And it's working, it shows my splash screen for 3 and a half seconds. But, when I'm starting the app, first the display is white for like a second and then it shows my splash screen. Since the code is working, could the problem be the mobile phone that I'm using instead an emulator? Or I need to add something to my code?

Upvotes: 1

Views: 112

Answers (3)

Deval
Deval

Reputation: 255

White screen is caused by AppTheme. When app is initialize it shows default white screen before setting any views.

You can make that white screen disappear by just adding the following attribute to your AppTheme.

Just add

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground"><place here any drawable or color></item>
</style>

Let me know if it is helpful to you. Thanks!!!

Upvotes: 1

Marcel Gangwisch
Marcel Gangwisch

Reputation: 9026

For my Splashscreen I'm using two activities which you also have to register in your manifest file:

<activity
        android:name=".ActivityMain"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name"
        android:parentActivityName=".SplashActivity"
        android:windowSoftInputMode="stateAlwaysHidden">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

In my Splash Activity I'm starting my main activity by an intent like following:

public class SplashActivity extends AppCompatActivity {

    private final Runnable task = () -> {
        Intent intent = new Intent(this, ActivityMain.class);
        startActivity(intent);
        finish();
    };

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

        final AnimatedVectorDrawableCompat animatedVectorDrawableCompat =
                AnimatedVectorDrawableCompat.create(getApplicationContext(),
                        R.drawable.animatedvector);

        ImageView imageView = findViewById(R.id.imageView);
        imageView.setImageDrawable(animatedVectorDrawableCompat);

        final Animatable animatable = (Animatable) imageView.getDrawable();
        animatable.start();

        Handler handler = new Handler();
        handler.postDelayed(task, 1500);
    }
}

As you can see, the start of the main activity is delayed by a handler.

Hope this helps; I dont get white screens with this code ;-)

Upvotes: 0

Nishant Garg
Nishant Garg

Reputation: 135

Nope It's not a problem of your code. It also happens with my app. Seems that the white screen comes when android is loading the app itself. and if you are loading a lot at start only then it will take longer to load and hence bigger white screen.

Upvotes: 0

Related Questions