user198989
user198989

Reputation: 4665

How to remove black screen when opening app

I have a splash screen which is being showed only on fresh start of app. If user hit back button and start app again the splash doesn't show. Everything is fine until here, if splash doesn't show, there is a 1-2 second black screen when opening app. Here is my splashactivity java file;

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) // if first time, show splash
{

    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("first_time", true);
    editor.commit();


    setContentView(R.layout.activity_splash);


    Thread t = new Thread() {
        public void run() {
            try {
                int time = 0;
                while (time < 4000) {
                    sleep(100);
                    time += 100;
                }
            }
            catch (InterruptedException e) {
                // do nothing
            }
            finally {

                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }
    };
    t.start();
}
else // if not first time, dont show splash
{
    setContentView(R.layout.activity_splash);
    Intent i = new Intent(SplashScreen.this, MainActivity.class);
    startActivity(i);
    finish();
}

How can I fix this issue ?

Upvotes: 2

Views: 3198

Answers (5)

Bryan
Bryan

Reputation: 15155

Because you are checking if it is the "first time" opening the app before showing the splash screen:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) {
    // Show splash
} else {
    // Don't show splash
}

This is not the right way to create a splash screen in the first place. You shouldn't make your users wait for the splash screen by creating a timer. Instead, you should only show the splash screen for as long as it takes your app to load.

To do this you should create a simple layer-list drawable to use as the background of your splash activity:

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

    <!-- A solid background fill -->
    <item
        android:drawable="@color/gray"/>
    <item>

    <!-- A centered logo -->
    <bitmap
        android:gravity="center"
        android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Then use this layer-list as the background for the theme you will use in your splash activity:

<resources>

    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <!-- Splash Screen theme -->
    <style name="SplashTheme">
        <item name="android:background">@drawable/background_splash</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>

</resources>

Apply that theme to your splash activity in the manifest:

<activity android:name=".SplashScreen"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

Then, finally, all you have to do in your splash activity is start your MainActivity:

public class SplashScreen extends AppCompatActivity {

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

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

Make sure you don't call setContentView() in your splash activity, as this will add unnecessary loading time to your splash screen.

Upvotes: 1

Rana.S
Rana.S

Reputation: 2275

You have to give splash screen background colour as your application main theme colour.

<activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Set style In Your values/style.xml `

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="SplashScreenTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
</style>

Set style in your values-v21/style.xml

 <style name="SplashScreenTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:colorPrimaryDark">@color/white</item>
</style>

Upvotes: 1

MatiRC
MatiRC

Reputation: 339

Adding @Bryan I recommend you the cold start. For Example i did like this

In your Style.xml:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

<style name="AppTheme.splashScreenLauncher">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

</style>

The splash_screen.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/white"/>

    <item>
        <bitmap
            android:src="@drawable/SPLASHIMAGE"
            android:gravity="center"/>
    </item>
</layer-list>

In the manifest, where you set your launcher activity:

android:theme="@style/AppTheme.splashScreenLauncher"> 

In your launcher activity:

setTheme(R.style.AppTheme); // IMPORTANT BEFORE super.onCreate
super.onCreate(savedInstanceState);

Upvotes: 2

yash786
yash786

Reputation: 1151

 /Just use the below code Snipeet/

Add following code in Style.xml file

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>

</style>

 And then use this style in AndroidManifest inside application Tag
 Like this: 
 android:theme="@style/Theme.Transparent"

Upvotes: 1

Bruno Ferreira
Bruno Ferreira

Reputation: 1571

Add this in app styles:

<item name="android:windowDisablePreview">true</item>

Upvotes: 1

Related Questions