Masih kelanjutan dari Android Project Part 2
Splash screen itu adalah tampilan awal sebelum program dijalankan. Splash screen juga bisa digunakan untuk mempercantik aplikasi yang dibuat supaya aplikasinya kelihatan lebih professional.
Langsung saja ke kodingannya ya,
1. Pertama kita buat splash.java
package com.hellonumber;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 2000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
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 newIntent=new Intent(Splash.this,HelloNumberActivity.class);
startActivityForResult(newIntent,0);
}
}
};
splashTread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
}
2. Selanjutnya kita buat splash.xml
Untuk splash.xml saya tambahin progressBar, supaya tampilannya lebih menarik :D
<?xml version="1.0" encoding="utf-8"?>NB : Untuk yang berwarna Ungu berarti saya membuat splash screen dari gambar dari folder drawable dengan nama splash.png.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|center_horizontal"
android:background="@drawable/splash">
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3. Terakhir untuk AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hellonumber"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Splash"
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=".HelloNumberActivity">
<intent-filter>
<action android:name="android.intent.action.HelloNumberActivity" />
</intent-filter>
</activity>
</application>
</manifest>
Yap, nih hasilnya kalo di run :
0 komentar:
Posting Komentar