Untuk membuat interaksi antar activity kita membutuhan langkah-langkah berikut.
1. XML Layout
Buat dua XML layout file di folder “res/layout/” :
res/layout/activity_main.xml
– Activity pertamares/layout/activity_main_2.xml
– Activity keduaFile : res/layout/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:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Pilih" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="Activity Pertama"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
File : res/layout/activity_main_2.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Kembali" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginBottom="35dp" android:text="Activity kedua" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
2. Activity
buat 2 buah activity class
1. MainActivity.java
2. Activity2.java
Untuk memindahkan dari satu activity ke activity yang lain kita dapat menggunakaan kode berikut.
Intent intent = new Intent(context, class activity tujuan.class);
startActivity(intent);
File : MainActivity.java
package com.smh.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
public Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickListener();
}
public void onClickListener(){
final Context context = this;
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
}
}
File : Activity2.java
package com.smh.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity2 extends Activity{
public Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
OnClickListener();
}
public void OnClickListener(){
final Context context = this;
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});
}
}
3. AndroidManifest.xml
deklarasikan class activity kedua pada
AndroidManifest.xml
File : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smh.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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:label="@string/app_name"
android:name=".Activity2" >
</activity>
</application>
</manifest>
Running Aplikasi
Download Source Code
download source code disini dan cara download tusfile disini
References
1. Activities Android
No comments:
Post a Comment