Friday, March 23, 2012

How to create a splash screen in android?


How to create a splash screen in android?

Hello everyone today i will show you how to create a splash screen in android.
This is one of the simplest ways to create a splash screen however there are another ways to create the splash screen.
Lets look at the code.
We need two layouts one for the splash screen and another for the first screen that comes after splash screen.
The splash screen layout will look like this.
splashscreen.xml
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <ImageView android:src="@drawable/android"
    android:layout_width="fill_parent"
     android:id="@+id/imageView1"
     android:layout_height="fill_parent"></ImageView>
</LinearLayout>
1
 
Now the <strong>main.xml</strong> file.
 
1
<?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:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Splash screen Demo from CoderzHeaven"
    />
</LinearLayout>
Now the main java file.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package pack.coderzheaven;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
 
public class SplashScreenDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        creatingSplashScreen();
    }
 
    private void createFirstScreen()
    {
               setContentView(R.layout.main);
    }
 
    private void creatingSplashScreen()
    {
         new CountDownTimer(5000, 1000) {
                   public void onTick(long millisUntilFinished)
             {
             }
 
             public void onFinish() {
                 createFirstScreen();
             }
          }.start();
    }
}
Make sure you have an image named “android.png” or “android.jpg” in your res/drawable folder

No comments: