An android app takes few seconds to startup, especially on its first boot. During this time a splash screen displays the startup progress or may display the branding information to identify and promote the application to the user.
Common Splash Screen Mistakes
A number of developers use the Splash screen to display images, themes, icons or pictures for a couple of seconds. I believe it's a bad idea to use icons and pictures in Splash screen.
What's the right way then?
Well, we must always use splash screen as the background theme for the activity. It is best advised not to create a layout file for Splash screen. Instead, specify activity’s theme background as splash layout.
The Splash Screen Implementation
Correct Splash Screen implementation involves the following step:
Step 1) Create XML drawable splash_background.xml inside res/drawable folder
<?xml version=1.0" encoding=utf-8"?>
<layer-list xmlns:android=http://schemas.android.com/apk/res/android">
<item android:drawable=@color/colorPrimary />
<item>
<bitmap
android:gravity=center
android:src=@mipmap/ic_launcher />
</item>
</layer-list>
Step 2) Now, set the above XML file as your splash activity's background theme. Add a new SplashTheme for your Splash Activity.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Splash Screen theme. -->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
</resources>
Step 3) Configure above created SplashTheme as your splash activity’s theme in your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidjavapoint.splashscreen">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
<activity android:name=".HomeActivity" />
</application>
</manifest>
Step 4) Using the Splash Activity without using layout file
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start home activity
startActivity(new Intent(SplashActivity.this, HomeActivity.class));
// close splash activity
finish();
}
}
Note: I have not used the setContextView() method for this activity. Activity view is displaying from the theme so it's a faster way to create splash screen than creating a layout.
How did you find the article? If you like it, please share it on Facebook, Twitter, and other social media accounts.
0 Comment(s)