I am using a dialog with 2 radio buttons to get the user to classify an object. This works if the user explicitly clicks on one of the radio buttons but not if they simply press OK straight away. How can I handle this situation?
case DIALOG_OBJECT_CLASSIFICATION:
return new AlertDialog.Builder(DrawNewPlans.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("What Object are you drawing?")
.setSingleChoiceItems(R.array.types_of_object, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
makeToast("WhichButton = "+whichButton);
switch (whichButton) {
case 0:
makeToast("User Picked Room");
isRoom = true;
isFurniture = false;
return;
case 1:
makeToast("User Picked Furniture");
isRoom = false;
isFurniture=true;
return;
}
/* User clicked on a radio button do some stuff */
}
})
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
makeToast("classOfRoom is "+classOfRoom);
makeToast("isRoom = "+isRoom);
makeToast("isFurniture = "+isFurniture);
if (isRoom == true && isFurniture == false) {
classOfRoom="Room";
} else if (isRoom == false && isFurniture == true) {
classOfRoom="Furniture";
}
/* User clicked Yes so do some stuff */
isClassified=true;
methodWhichStartsDialogs();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
/* User clicked No so do some stuff */
}
}).create();
}
I just added the Toasts for debugging. Many Thanks.
You can either set one radio button to be true by default, that is even if the user doesn’t select anything, that option will be selected by default.
Or, otherwise, you can check if atleast one of the radio buttons are selected when the user presses okay. If none has been selected, do not dismiss the dialog.