Jumat, 29 Juni 2012

Pemrograman Android : Membuat Splash Screen pada Android

Splash Screen adalah sebuah halaman yang biasa muncul ketika suatu program pertama kali muncul. Bisa digunakan untuk memberi identitas program, nama dan logo, sekaligus menanti program selesai loading.

Pada contoh ini untuk membuat Splash Screen di Android kita membutuhkan 2 Activity dan 2 Layout Buatlah 2 Layout Tampilan

splash.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:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="SPLASH SCREEN"
    />
</LinearLayout>





dan

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:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="HALAMAN UTAMA"
    />
</LinearLayout>


kemudian dibuat pula 2 Activity

SplashActivity.java


package my.amijaya.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SplashActivity  extends Activity {
protected boolean _active = true;
protected int _splashTime = 5000; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    Intent i;
                    i = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(i); 
                    stop();
                }
            }
        };
        splashTread.start();
    }
}


dan

MainActivity.java


package my.amijaya.splashscreen;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


Jangan lupa mendefinisikan Layout yang akan jalan terlebih dahulu dan layout satunya di Android Manifest

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="my.amijaya.splashscreen"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SplashActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".MainActivity"
                  android:label="Halaman Utama">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 


Kode Program selengkapnya dapat didownload dibawah ini :

https://docs.google.com/open?id=0B4i1FYc_4RXzX1BrRFpkMWtqcDQ

Semoga berguna