สร้างโปรเจ็ค Android Studio โดยจะประกอบไปด้วย
- MainActivity.java
- SplashScreen.java
ส่วนนี้จะเป็นส่วนของโค้ด
- activity_main.xml
- splashsceen.xml
ส่วนนีจะเป็นส่วนของการออกแบบหน้าจอ
ก่อนอื่นออกแบบหน้าจอที่เป็นหน้าแรกก่อน
โค้ดใน splashsceen.xml
โค้ด: เลือกทั้งหมด
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3b4534" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/toon" />
</RelativeLayout>
โค้ด: เลือกทั้งหมด
android:background="#3b4534" >
_______________________________________
โค้ด: เลือกทั้งหมด
android:src="@drawable/queen" />
_______________________________________
โค้ดใน SplashScreen.java
โค้ด: เลือกทั้งหมด
public class SplashScreen extends MainActivity
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable)){
@Override
public void run() {
Intent splashIntent = new Intent(SplashScreen.this,MyActivity.class);
SplashScreen.this.startActivity(splashIntent);
SplashScreen.this.finish();
}
},SPLASH_TIME_OUT);
}
}
*กำหนดไว้ SPLASH_TIME_OUT = 3000 หรือประมาณ 3 วินาที 1000 = 1 วินาที
____________________________________________________________________
ในหน้า AndroidManifest.xml แทรก Activity ที่จะทำการ รัน เป็นหน้าแรก SplassScreen
โค้ด: เลือกทั้งหมด
<activity android:name=”.SplashScreenActivity” 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”>
</activity>
โค้ดใน activity_main.xml
โค้ด: เลือกทั้งหมด
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3b92d1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Main Page"
android:textColor="#BBBBBB"
android:textSize="10sp" />
</RelativeLayout>
โค้ดใน MainActivity.java
โค้ด: เลือกทั้งหมด
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
}