There is 2 ways (at least) to create a splash screen :
The first is to create a splash activity displaying a layout and then transfert the user to the main activity. Here the SplashActivity.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class SplashActivity extends AppCompatActivity { private static int SPLASH_DURATION = 2000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_activity); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); finish(); } },SPLASH_DURATION); } } |
Note : the splash screen is displayed during 2 seconds (2000 ms) before going to the main activity. You can tune the SPLASH_DURATION constant to change the duration.
In res/layout/splash_activity.xml you will have something like this :
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="splash screen" android:scaleType="centerCrop" app:srcCompat="@drawable/splash_screen"> </ImageView> |
You will put the image to display in res/drawable/splash_screen.png
And that’s all. The advantage of this method is that you can have exactly the layout you want. The drawback is that it does not appear instantly : you will have to wait half a second.
If you have just a small logo to display, the best wait is… NOT display layout, instead, you just have to add a theme to your AndroidManifest.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xxxxxxxx"> .. <application .. <activity android:name=".SplashActivity" android:label="@string/app_name" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" /> ... </application> </manifest> |
res/values/styles.xml will be like this :
1 2 3 4 5 6 7 |
<resources> ... <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowBackground">@drawable/background_splash</item> </style> </resources> |
The picture will be defined in res/drawable/background_splash.xml :
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/splashbackgroundcolor"/> <item> <bitmap android:gravity="center" android:src="@drawable/splash_screen" /> </item> </layer-list> |
You will put the image to display in res/drawable/splash_screen.png
and the background color in res/values/colors.xml :
1 2 3 4 5 |
<?xml version="1.0" encoding="utf-8"?> <resources> ... <color name="splashbackgroundcolor">#2E2277</color> </resources> |
And the most important to have the splash screen to be instantly displayed is to NOT display the layout, here I just commented out the layout setting inĀ SplashActivity.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class SplashActivity extends AppCompatActivity { private static int SPLASH_DURATION = 2000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.splash_activity); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); finish(); } },SPLASH_DURATION); } } |