I took a far different approach with a splash screen for one of my apps because it should be ready immediately, even before a layout is inflated. So instead of using a layout, I specified the splash screen's background as the activity's theme background.
So, I changed the main activity to be
SplashActivity
in the
AndroidManifest.xml
file, like:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The background/theme for the
SplashActivity
looks like:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/gray"/>
<item>
<bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
The
SplashActivity
class then looks like:
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
Then in
MainActivity
, do whatever you want as normal. May not be exactly what you are looking for, but it is the way Google is doing this now, too.