android, coding, dunia it,

Custom Dialog dengan Array

Hangga Aji Sayekti Hangga Aji Sayekti Follow May 29, 2013 · 1 min read
Share this

Di Android, kita juga dapat membuat alert dialog yang bentuknya seperti pada gambar diatas. Yuk, langsung menuju TKP.

custom-dialog

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

Kemudian saya membuat method baru yg saya beri nama ShowCustomDialog()

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();
}

}

</input>

Hangga Aji Sayekti
Written by Hangga Aji Sayekti
A passionate Software Engineer from Indonesia with experience since 2008. Bachelor's degree in Mathematics and loves Science and Computers.