Android – blank screen comes before splash

androidsplash-screen

The main problem is the splash screen appears after 2-3 seconds. Before splash screen a blank layout appears which I don't want. Otherwise it runs fine. Just want to remove the blank layout which appears before the splash page.

MainActivity:

public class MainActivity extends Activity {

    private static String TAG = MainActivity.class.getName();
    private static long SLEEP_TIME = 5; // Sleep for some time

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

        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  // Removes notification bar

        setContentView(R.layout.activity_main);

        // Start timer and launch main activity
        IntentLauncher launcher = new IntentLauncher();
        launcher.start();
    }

    private class IntentLauncher extends Thread {

        @Override
        /**
         * Sleep for some time and than start new activity.
         */
        public void run() {
            try {
                // Sleeping
                Thread.sleep(SLEEP_TIME*1000);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }

            // Start main activity
            Intent intent = new Intent(MainActivity.this, Login.class);
            MainActivity.this.startActivity(intent);
            MainActivity.this.finish();
        }
    }

}

main layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/splash"
    tools:context=".MainActivity" >

</RelativeLayout>

Best Solution

Generally speaking, splash screens are not recommended for an app but if you really must.

Android will load a blank layout before it loads an activity layout based on the theme you have set for it. The solution is to set the theme of the splash activity to a transparent one.

Create a transparent theme in res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="Theme.Transparent" parent="android:Theme">
      <item name="android:windowIsTranslucent">true</item>
      <item name="android:windowBackground">@android:color/transparent</item>
      <item name="android:windowContentOverlay">@null</item>
      <item name="android:windowNoTitle">true</item>
      <item name="android:windowIsFloating">true</item>
     <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

Then set the theme in your manifest

<activity android:name=".SplashActivity" android:theme="@style/Theme.Transparent">
...
</activity>
Related Question