Make Android application of blog part 1: Android Splash Screen


android learning hub, androidlearninghub, splash screen, android studio, android application of blog
Android Splash Screen is used to show the user a screen before app loads. Following are the reasons developers used the splash screen in their applications.

  • Load data in the background
  • Promote logo
  • Promote app name with the owner

To implement this create a basic android app with blank activity using Android Studio.

Your current Activity is your landing page i.e. the page where the user will land after the splash screen.

Now right click on app -> new -> Activity -> Blank Activity. and named it SplashActivity.

android learning hub, splash screen, blank activity

In activity_splash.xml change below code to display logo and company name.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="@color/colorPrimaryDark" >

    <ImageView        
 android:id="@+id/imgLogo"        
 android:layout_width="wrap_content"        
 android:layout_height="wrap_content"        
 android:layout_centerInParent="true"        
 android:src="@drawable/icon" />

    <TextView        
 android:layout_width="fill_parent"        
 android:layout_height="wrap_content"        
 android:layout_marginBottom="10dp"        
 android:textSize="12dp"        
 android:textColor="#454545"        
 android:gravity="center_horizontal"        
 android:layout_alignParentBottom="true"        
 android:text="Android Learning Hub" />

</RelativeLayout>

In SplashActivity.java change below code to implement to set splash screen for 4 seconds.

package com.praval.androidlearninghub;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;

    @Override    
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {

            @Override            
   public void run() {
                // This method will be executed once the timer is over                
    // After 4 seconds app will move from Splash screen to home page of your app                
    Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);

                // close this activity                
    finish();
            }
        }, SPLASH_TIME_OUT);
    }
}


Run the application, you will not see your splash screen instead of it you will see the main screen. Because Android OS will treat an activity as launcher activity which is defined in AndroidManifest.xml. So, open AndroidManifest.xml use below code in splash activity.

<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

So your AndroidManifest will look like below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    
package="com.praval.androidlearninghub">

    <application        
 android:allowBackup="true"        
 android:icon="@mipmap/ic_launcher"        
 android:label="@string/app_name"        
 android:supportsRtl="true"        
 android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
        </activity>
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

you will see the splash screen with logo and company name for 4 seconds and then it will display the main activity page in the application.

android learning hub, splash screen, blank activity, android activity


Read more about android development.

  1. Android calendar view
  2. Android Google Map v2
  3. First Android application using Eclipse



Comments