Question: 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 limit but not able to disable the checkboxes

```
 AlertDialog.Builder builder=new AlertDialog.Builder(this);
 View view=inflater.inflate(R.layout.dialog_genre,null,false);
 builder.setView(view).setTitle("Select Genres");

 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{
                        Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show();
                    }
            }
            else if (selectedgenre.contains(String.valueOf(which)))
            {
             selectedgenre.remove(String.valueOf(which));
             checkedGenres[which]=false;
        }
    }
}

```

[![enter image description here](https://i.stack.imgur.com/OWJDD.png)](https://i.stack.imgur.com/OWJDD.png)

Answer: 
Views inside a ListView are reusable, so [items aren't really linked to a view](https://stackoverflow.com/a/6923513/9179761). You can however enable or disable a view when it's being added to the ListView inside your AlertDialog's ListView. [ViewGroup.setOnHierarchyChangeListener](https://developer.android.com/reference/android/view/ViewGroup.html#setOnHierarchyChangeListener(android.view.ViewGroup.OnHierarchyChangeListener)) 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)](https://i.stack.imgur.com/QbLWT.jpg)](https://i.stack.imgur.com/QbLWT.jpg)

Note that `setEnable` won't avoid clickListeners to trigger. If you don't want to get your checkbox clicked add `child.setOnClickListener(null)`.
```
   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