Reputation:
I've got the following code:
final AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setTitle(R.string.str_dbox_remove_plant_title);
final HashMap<String, Integer> stuff = myCollection.getListOfStuffWithLocation();
final CharSequence[] items = stuff.keySet().toArray(new CharSequence[stuff.size()]);
final List<Integer> indexesOfSelectedStuff = new ArrayList<Integer>();
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
indexesOfSelectedStuff.add(stuff.get(items[which].toString()));
else if (indexesOfSelectedStuff.contains(stuff.get(items[which].toString())))
indexesOfSelectedStuff.remove(which);
}
});
builder.setPositiveButton(R.string.str_dbox_remove_stuff_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
for (int i = 0; i < indexesOfSelectedStuff.size(); i++) {
myCollection.removeStuff(i);
}
}
});
builder.setNegativeButton(R.string.str_dbox_remove_stuff_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setCancelable(false); // so as to prevent the back button from closing the dialog box
final AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(false);
alert.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); // by default, disable OK button
alert.show();
I have an AlertDialog
with some CheckBox
es and I would like to disable the PositiveButton
as long as there aren't any CheckBox
es ticked.
I have a List
called indexesOfSelectedStuff
and you could say that in some way, using its size()
method will give me an idea of how many CheckBoxes
are selected. However, I am not sure where to place it or how to reference the PositiveButton
within the builder. I wanted to place it on the onClick
method (as defined in the OnMultipleChoiceClickListener()
), like so:
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
indexesOfSelectedStuff.add(stuff.get(items[which].toString()));
else if (indexesOfSelectedStuff.contains(stuff.get(items[which].toString())))
indexesOfSelectedStuff.remove(which);
if (indexesOfSelectedStuff.size() == 0) {
// disable button
// but problem is, I don't know how to reference the POSITIVE_BUTTON
}
else {
// enable button
}
}
I know there's a high probability that I can't insert it there. And that I'd have to do the checks after the AlertDialog
is created. But if that is the case, what would you recommend that I use? I'm thinking of Listener
s but which? And how should I go about it?
Upvotes: 2
Views: 993
Reputation: 1466
For this you need to create a custom dialog. To know how check this link. Click Here
You could try this.
Create a boolean as true. If the check box is ticked set the Boolean value true false.
Then whenever you're creating an alert dialogue simply check if the Boolean is set to false or true. If true create a yes button else don't create a yes button. Now the problem is that the check button is inside alertdialog am I right if so.
Every time a check button is clicked you could close the Alert dialogue and open it again with the checkboxes already ticked ticked and with no yes button. That is use the referenced button to set disabled. Or in technical terms setEnabled = "false;"
Of course this is laggy etc but this is all I can do. Sorry
Upvotes: 1