Create A Custom Dialog With Radio Buttons List
I've got a method in which i have a list of values: /** * ISO * */ public void getISO(View view) { // Open dialog with radio buttons List
Solution 1:
best and easy way......
voiddialog(){
AlertDialog.Builderalt_bld=newAlertDialog.Builder(this);
//alt_bld.setIcon(R.drawable.icon);
alt_bld.setTitle("Select a Group Name");
alt_bld.setSingleChoiceItems(grpname, -1, newDialogInterface
.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(),
"Group Name = "+grpname[item], Toast.LENGTH_SHORT).show();
dialog.dismiss();// dismiss the alertbox after chose option
}
});
AlertDialogalert= alt_bld.create();
alert.show();
///// grpname is a array where data is stored...
}
Solution 2:
Call showRadioButtonDialog()
from the button.
This is just an example:
privatevoidshowRadioButtonDialog() {
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_layout);
List<String> stringList=new ArrayList<>(); // here is listfor(int i=0;i<2;i++) {
if (i==0){
stringList.add("Number Mode");
}else {
stringList.add("Character Mode");
}
}
RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radioGroup);
for(int i=0;i<stringList.size();i++){
RadioButton rb=new RadioButton(this); // dynamically creating RadioButton and adding to RadioGroup.
rb.setText(stringList.get(i));
rg.addView(rb);
}
}
Your layout view might be:radiobutton_dialog.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RadioGroupandroid:id="@+id/radio_group"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="center_vertical"android:orientation="vertical"></RadioGroup></LinearLayout>
Note: you can customize your dialog view (like setting title, message etc.)
Edit:
To retrieving value of the selected RadioButton
you have to implement setOnCheckedChangeListener
listener for your RadioGroup
as :
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
publicvoidonCheckedChanged(RadioGroup group, int checkedId) {
int childCount = group.getChildCount();
for (int x = 0; x < childCount; x++) {
RadioButton btn = (RadioButton) group.getChildAt(x);
if (btn.getId() == checkedId) {
Log.e("selected RadioButton->",btn.getText().toString());
}
}
}
});
Solution 3:
A clean way is like this:
http://developer.android.com/guide/topics/ui/dialogs.html
Excerpt from (Adding a persistent multiple-choice or single-choice list)
mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle(R.string.pick_toppings)
// Specify the list array, the items to be selected by default (null for none),// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
publicvoidonClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} elseif (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere// or return them to the component that opened the dialog
...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog, int id) {
...
}
});
return builder.create();
No custom view is necessary.
Solution 4:
This worked for me:
final CharSequence[] items = {"Option-1", "Option-2", "Option-3", "Option-4"};
AlertDialog.Builderbuilder=newAlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView and Radio button");
builder.setIcon(R.drawable.icon);
builder.setSingleChoiceItems(items, -1, newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
AlertDialogalert= builder.create();
alert.show();
Solution 5:
Kotlin version:
fundialog() {
val options = arrayOf("option1", "option2")
var selectedItem = 0val builder = AlertDialog.Builder(this)
builder.setTitle("Select an option")
builder.setSingleChoiceItems(options
, 0, { dialogInterface: DialogInterface, item: Int ->
selectedItem = item
})
builder.setPositiveButton(R.string.accept, { dialogInterface: DialogInterface, p1: Int ->
Toast.makeText(getApplicationContext(),
"selected item = " + options[selectedItem], Toast.LENGTH_SHORT).show();
dialogInterface.dismiss()
})
builder.setNegativeButton(R.string.cancel, { dialogInterface: DialogInterface, p1: Int ->
dialogInterface.dismiss()
})
builder.create()
builder.show();
}
Post a Comment for "Create A Custom Dialog With Radio Buttons List"