Search code examples
androidandroid-alertdialogmultichoiceitems

Android AlertDialog Multi Choice Items


I have an activity for user with a button called choose days when user clicks the button it show a alert dialog for selecting the day of week

now i want when user select multiple days

then previously selected check box will show in check position and remains with uncheck position


Solution

  • Here is a nice library for that, I use it for my project. enter image description here

    Step 1:

    Add these lines to your build.gradle file

      dependencies {
    compile 'com.afollestad:material-dialogs:0.7.7.0'
    }
    
    repositories 
    maven { url 'https://dl.bintray.com/drummer-aidan/maven' }
    }
    

    Step 2: In code where you want to show dialog add this:

    new MaterialDialog.Builder(this)
        .title(R.string.title)
        .items(R.array.items)
        .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
            @Override
            public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                /**
                 * If you use alwaysCallMultiChoiceCallback(), which is discussed below,
                 * returning false here won't allow the newly selected check box to actually be selected.
                 * See the limited multi choice dialog example in the sample project for details.
                 **/
                 return true;
            }
        })
        .positiveText(R.string.choose)
        .show();
    

    items(R.array.items) - that should be an array of string values in your resources.

    P.S. Please read the documentation of this library, it very clear and simply)

    enter image description here