WarnaActivity.java
package app.warna.hw;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
public class WarnaActivity extends Activity {
/** Called when the activity is first created. */
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CharSequence[] warna = {"Pink", "Biru", "Hijau", "Merah"};
AlertDialog.Builder builder = newAlertDialog.Builder(this);
builder.setTitle("Pilih warna kesukaan mu ");
builder.setItems(warna, newDialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, intitem) {
Toast.makeText(getApplicationContext(), warna[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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="Contoh penggunaan dari AlertDialog by vhyemuuth"
/>
</LinearLayout>
Tidak ada komentar:
Posting Komentar