How to Set SplashScreen in Android

Procedure
  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create two Java files, one is MainActivity.java file and the other is A.java.
  4. Create two XML files, one is activity_main.xml file and the other is home.xml for layout design.
  5. Declare a variable of the TimerTask.
  6. Then declare a TimerTask function like this:
  1. TimerTask ttt=new TimerTask() {  
  2.       public void run() {  
  3.          Intent i=new Intent(getApplicationContext(),A.class);  
  4.          startActivity(i);  
  5.       }  
  6.    };  
  7.    Timer t=new Timer();  
  8.    t.schedule(ttt, 4000);  
  9.  
The following is the code.
  1. MainActivity.java  
  2. package com.example.splashscreenn;  
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.view.Menu;  
  9. public class MainActivity extends Activity {  
  10.  TimerTask ttt;  
  11.  @Override  
  12.  protected void onCreate(Bundle savedInstanceState) {  
  13.   super.onCreate(savedInstanceState);  
  14.   setContentView(R.layout.activity_main);  
  15.   ttt = new TimerTask() {  
  16.    @Override  
  17.    public void run() {  
  18.     Intent i = new Intent(getApplicationContext(), A.class);  
  19.     startActivity(i);  
  20.    }  
  21.   };  
  22.   Timer t = new Timer();  
  23.   t.schedule(ttt, 4000);  
  24.  }  

A.java
  1. package com.example.splashscreenn;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class A extends Activity {  
  5.  @Override  
  6.  protected void onCreate(Bundle savedInstanceState) {  
  7.   super.onCreate(savedInstanceState);  
  8.   setContentView(R.layout.home);  
  9.  }  

activity_main.xml
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.       
  5.       android:layout_width="match_parent"  
  6.       android:layout_height="match_parent"  
  7.       android:background="#ff0000"  
  8.       android:paddingBottom="@dimen/activity_vertical_margin"  
  9.       android:paddingLeft="@dimen/activity_horizontal_margin"  
  10.       android:paddingRight="@dimen/activity_horizontal_margin"  
  11.       android:paddingTop="@dimen/activity_vertical_margin"  
  12.       tools:context=".MainActivity" >  
  13.       
  14.     <TextView  
  15.          android:layout_width="wrap_content"  
  16.          android:layout_height="wrap_content"  
  17.          android:text="Abhijeet" />  
  18.            
  19. </RelativeLayout> 
home.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.       android:layout_width="match_parent"  
  6.       android:layout_height="match_parent"  
  7.       android:orientation="vertical" >  
  8.       
  9.     <Spinner  
  10.       android:id="@+id/spinner1"  
  11.       android:layout_width="match_parent"  
  12.       android:layout_height="wrap_content" />  
  13.         
  14.     <Spinner  
  15.       android:id="@+id/spinner2"  
  16.       android:layout_width="match_parent"  
  17.       android:layout_height="wrap_content" />  
  18.         
  19.     <Spinner  
  20.       android:id="@+id/spinner3"  
  21.       android:layout_width="match_parent"  
  22.       android:layout_height="wrap_content" />  
  23.         
  24. </LinearLayout>  
Output
 
 


Similar Articles