Pertama, ubah layout di layout-main.xml menjadi seperti berikut ini :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Tampil Alert" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick2" android:text="Tampil Toast" /> </LinearLayout>
Kemudian buka file Activity.java, ubah menjadi berikut ini :
package com.amijaya.messagebox; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class ContohActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.setPositiveButton("Setuju", new OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(this, "Ok Boss", Toast.LENGTH_LONG).show(); } }); dialog.setNeutralButton("No Comment", new OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(this, "No Comment Boss", Toast.LENGTH_LONG).show(); } }); dialog.setNegativeButton("Larii", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }); dialog.show(); } public void onClick2(View view) { Toast.makeText(this, "Yuhuu Toast", Toast.LENGTH_LONG).show(); } }
Jalankan aplikasi. Kemudian coba klik Tombol-Tombolnya.