Di Android, kita juga dapat membuat alert dialog yang bentuknya seperti pada gambar diatas. Yuk, langsung menuju TKP.
0 1 2 |
import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; |
Kemudian saya membuat method baru yg saya beri nama ShowCustomDialog()
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
private void ShowCustomDialog(){ final CharSequence[] pilihChar = {"Satu", "Dua", "Tiga"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Pilihan").setItems(pilihChar, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch(which){ case 0 : Toast.makeText(MainActivity.this, "Pilih : "+pilihChar[0], Toast.LENGTH_SHORT).show(); break; case 1 : Toast.makeText(MainActivity.this, "Pilih : "+pilihChar[1], Toast.LENGTH_SHORT).show(); break; case 2 : Toast.makeText(MainActivity.this, "Pilih : "+pilihChar[2], Toast.LENGTH_SHORT).show(); break; } } }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); builder.create(); builder.show(); } [/code] Berikut Source lengkapnya [code language="java"]package com.nyekrip.buttonevent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; public class MainActivity extends Activity { private Button myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(ButtonKlik); } private OnClickListener ButtonKlik = new OnClickListener() { @Override public void onClick(View v) { ShowCustomDialog(); } }; private void ShowCustomDialog(){ final CharSequence[] pilihChar = {"Satu", "Dua", "Tiga"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Setting").setItems(pilihChar, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch(which){ case 0 : Toast.makeText(MainActivity.this, "Pilih : "+pilihChar[0], Toast.LENGTH_SHORT).show(); break; case 1 : Toast.makeText(MainActivity.this, "Pilih : "+pilihChar[1], Toast.LENGTH_SHORT).show(); break; case 2 : Toast.makeText(MainActivity.this, "Pilih : "+pilihChar[2], Toast.LENGTH_SHORT).show(); break; } } }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); builder.create(); builder.show(); } } |