Skip to content Skip to sidebar Skip to footer

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 = new CharSequence[]
            {"9×19mm Sidearm", ".40 Dual Elites", "228 Compact", "Night Hawk .50C"};
    boolean[] checkedItems = new boolean[]{false, false, true, true};
    final boolean[] disabledItems = new boolean[]{false, true, false, true};

    Context context = this;
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    dialogBuilder.setTitle("Select your pistol");

    dialogBuilder.setMultiChoiceItems(items, checkedItems,
            new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i, boolean checked) { 

        }
    });

    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.getListView().setOnHierarchyChangeListener(
            new ViewGroup.OnHierarchyChangeListener() {
        @Override
        public void onChildViewAdded(View parent, View child) {
            CharSequence text = ((AppCompatCheckedTextView)child).getText();
            int itemIndex = Arrays.asList(items).indexOf(text);
            child.setEnabled(disabledItems[itemIndex]);
        }

        @Override
        public void onChildViewRemoved(View view, View view1) {
        }
    });

    alertDialog.show();

Outcome of code sample (Android 9, API 28)

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
        public void 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();
                    }
            }
            else if (selectedgenre.contains(String.valueOf(array[which])))
            {
                selectedgenre.remove(String.valueOf(array[which]));
                checkedGenres[which]=false;
            }
        }
    }

Solution 3:

   builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(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"