Tuesday 10 February 2015

How to create the Customized Splash Screen in Android


1.      Create a customized splash screen layout

      a.       Layout having the TextView with the splash screen title
      b.      ImageView having the Loading image


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Layout : main.xml
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:background="#fff"
    android:gravity="center"
    android:layout_height="fill_parent">
   
 <TextView
android:id="@+id/Title"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:padding="10dp"
               android:textSize="30sp"
               android:textColor="#cd6000"
               android:textStyle="bold"
               android:text="My Splash Page" />
              
 <ImageView
android:id="@+id/Loading"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/loading" />
</LinearLayout>

2.      Create a rotate.xml in a drawable folder for need of  loading animation

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/linear_interpolator"   
    android:duration="1000" />

3.      Source SplashScreen.java

       a.  Referencing the xml widgets in to code.
 b.  Create a Animation and start load the animation to the ImageView.
       c.     Using the handler class and runnable thread make postdelay for 1000 milliseconds   and clear the animation then start the second activity.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Rajendhiran. E
 * Feb 10, 2013 11:10:52 PM
 */

public class SplashScreen extends Activity
{
   TextView Title;
   ImageView Loading;
   Animation anim;
   Handler h;
  
   protected void onCreate(Bundle savedInstanceState)
   {    
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         init();
         process();
   }
  
/* @author Rajendhiran. E, Feb 10, 2013 11:14:24 PM  */ 

   private void init()
   {
         Title = (TextView) findViewById(R.id.Title);
         Loading = (ImageView) findViewById(R.id.Loading);
         anim = AnimationUtils.loadAnimation(SplashScreen.this, 
R.drawable.rotate);
         Loading.startAnimation(anim);
         h = new Handler();
   }    

   /* @author Rajendhiran. E, Feb 10, 2013 11:19:35 PM */        
   private void process()
   {
         h.postDelayed(new  Runnable()
         {
               public void run()
               {                      
                     Loading.clearAnimation();
                     finish();
startActivity(new Intent(SplashScreen.this,SecondActivity.class));
               }
         }, 1000);        
   }    
}
Source Code: SplashScreen.zip

No comments: