Tuesday 8 March 2016

Android Splash Screen

A splash screen is a graphical control element consisting of window containing an image, a logo and the current version of the software. A splash screen usually appears while a game or program is launching. - Wikipedia
 
What are splash screens really for? Does my app need it? this must be questions you should ask your self before implementing a splash screen in your android applications. I'll try to answer this questions for you below.

What are splash screens really for?

Splash screens sometimes refereed as Launch screens normally are used to make the application user wait and not make him feel annoyed displaying them some thing good to see until the application gets initialized.For example some complex applications, games that have to caches some heavy graphics, make a network call at app launch or do something time consuming with no UI. Splash screens are also used in branding the application where the developer can display there brand logo and other stuff forcing the user to wait.

Does my app need it?

With reference to the first answer you must be now able to decide whether my app has such requirement at the startup/launch or not. If you want to use the splash only for branding you must think twice before you use it as it would be just a waste of the users time there are a lot of other things that where you can brand you app such as the action bar the drawer etc. Why waste the users time unless it is utterly important.

Now Lets start with the Implementation of the splash screen in the right way.
You can download the entire code here. 

First lets make a drawable resource file for the background

splash_background.xml
<?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>
 
Edit you style.xml and add the following theme

<style name="AppTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
</style>

What we did above is changed the default backgroung of the theme to the one we created. Now just apply this theme to you Splash Activity in the manifest file.

<activity
     android:name=".SplashActivity"
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
 
 This is what your activity_slpash.xml might look
 
<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:context="avinash.pet.project.splashscreendemo.SplashActivity">

</RelativeLayout>
 
The last part would be the Java activity code

SplashActivity.java
public class SplashActivity extends AppCompatActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        //start the next activity 
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
 
Now if we look for difference between the code here and most of the other code tutorials splash screen you notice we have not edited the splash activity xml file instead we edited the theme because loading of the xml file is done after the initialization of the application showing a glitch of a blank or white screen but thats not the case with the theme resource. You may also notice that we have not even added a timer but still the app shows the screen for significant time thats because it initializes. In order to  make the splash screen stay for longer time you may add a timer according to your need.

You can download the entire code here



No comments:

Post a Comment