Disable Checkbox Items In Alertdialog
I am giving array to select multiple options through alert dialog I want to disable selection if array is full I have been able to stop adding items to array after it reaches the l
Solution 1:
Views inside a ListView are reusable, so items aren't really linked to a view. You can however enable or disable a view when it's being added to the ListView inside your AlertDialog's ListView. ViewGroup.setOnHierarchyChangeListener lets you to do so.
final CharSequence[] items = newCharSequence[]
{"9×19mm Sidearm", ".40 Dual Elites", "228 Compact", "Night Hawk .50C"};
boolean[] checkedItems = newboolean[]{false, false, true, true};
finalboolean[] disabledItems = newboolean[]{false, true, false, true};
Contextcontext=this;
AlertDialog.BuilderdialogBuilder=newAlertDialog.Builder(context);
dialogBuilder.setTitle("Select your pistol");
dialogBuilder.setMultiChoiceItems(items, checkedItems,
newDialogInterface.OnMultiChoiceClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i, boolean checked) {
}
});
AlertDialogalertDialog= dialogBuilder.create();
alertDialog.getListView().setOnHierarchyChangeListener(
newViewGroup.OnHierarchyChangeListener() {
@OverridepublicvoidonChildViewAdded(View parent, View child) {
CharSequencetext= ((AppCompatCheckedTextView)child).getText();
intitemIndex= Arrays.asList(items).indexOf(text);
child.setEnabled(disabledItems[itemIndex]);
}
@OverridepublicvoidonChildViewRemoved(View view, View view1) {
}
});
alertDialog.show();
Note that setEnable
won't avoid clickListeners to trigger. If you don't want to get your checkbox clicked add child.setOnClickListener(null)
.
Solution 2:
okay I have solved the issue, after two days I came to know disabling checkbox is lengthy and hectic process but we can prohibit the selection, and that's what I wanted to do. here is my solution
builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() {
int count = 0;
@Override
publicvoid onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked) {
if (!selectedgenre.contains(String.valueOf(array[which])))
if(selectedgenre.size()<5)
{
selectedgenre.add(String.valueOf(array[which]));
checkedGenres[which]=true;
}
else{
count--;
((AlertDialog) dialog).getListView().setItemChecked(which, false);
checkedGenres[which]=false;
Toast.makeText(getApplicationContext(),"you can't add this genre",Toast.LENGTH_LONG).show();
}
}
elseif (selectedgenre.contains(String.valueOf(array[which])))
{
selectedgenre.remove(String.valueOf(array[which]));
checkedGenres[which]=false;
}
}
}
Solution 3:
builder.setMultiChoiceItems(array, checkedGenres, newDialogInterface.OnMultiChoiceClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked) {
if (!selectedgenre.contains(String.valueOf(which)))
if(selectedgenre.size()<5)
{
selectedgenre.add(String.valueOf(which));
checkedGenres[which]=true;
}
else{
//set your checkbox to false here//yourCheckbox.setchecked(false); Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show();
}
}
else
{
selectedgenre.remove(String.valueOf(which));
checkedGenres[which]=false;
}
}
}
Try with this.. issue in your else part
Solution 4:
In your else part put clickable false on your checkbox and also change the color so that user can identify it.
Post a Comment for "Disable Checkbox Items In Alertdialog"