Monday, 6 May 2019

How to make splash screen with xml layout file and remove white screen or make splash screen using style file and drawable file

First of all make one resource file into drawable file with xml file extension for background of splash screen. Here you can make some animation for better user experience.

drawable/splash_background.xml

Note :- Here .xml file name always lower case of _ .

<?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>
 
 
 Here drawable color from values of color.xml file and 
@mipmap/ic_launcher is Android 
Application app Icon.
 
Here we can move to next step. Here you can set background 
file as a background theme 
for Splash Activity into AndroidManifest.xml.
 
<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.AkhariPoint.splashscreen"
 
    <application 
       android:allowBackup="true" 
       android:icon="@mipmap/ic_launcher" 
       android:label="@string/app_name" 
       android:supportsRtl="true" 
       android:theme="@style/AppTheme"
 
       <!--Here below activity is main or launcher 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
     <!--Here another simple activity-->
     <activity android:name=".HomeActivity" /> 
 
  </application>
</manifest


Here we can move to next step. For set Splash screen .java file and 
some time delay
 for take some time at splashScreen and redirect to next Activity.
 
package com.AkhariPoint.splashscreen; 
 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity;
 
public class SplashActivity extends AppCompatActivity { 
 
  private Handler mWaitHandler; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) {         
   super.onCreate(savedInstanceState); 
     mWaitHandler = new Handler();
   
     mWaitHandler.postDelayed(new Runnable() {

        @Override
        public void run() { 
     
         try{
          // Start home activity         
          startActivity(new Intent(SplashActivity.this, HomeActivity.class)); 
          // close splash activity 
          finish(); 
        }catch (Exception ignored) {
          ignored.printStackTrace();
        }

       }
     }, 5000); 
  }
 
@Override
 public void onDestroy() {
   super.onDestroy();
        
   //Remove all the callbacks otherwise navigation will execute even 
   //after activity is killed or closed. 
    mWaitHandler.removeCallbacksAndMessages(null);
  } 
}
 
 
Notice that we do not have setContentView() for this SplashActivity. View is displaying from the theme and this way it is faster than creating a layout.
If you look at the time splash screen displays is exactly the same with the time taken by app to configure itself because of a cold launch (very first launch). If the app is cached, the splash screen will go away almost immediately.

Thanks 
Happy code.
 
  

No comments:

Post a Comment