Question

I have a MultiChoice AlertDialog where I have 25 choices.

I want to let the user select only any 5 out of 25.

When she selects the 6th choice I want to unckeck it and show a Toast message which says that only 5 choices she can make.

Is it possible with MultiChoice AlertDialog? Please help!

Was it helpful?

Solution 2

Create a static variable "count" and increment it on the option selected, and decrement when its deselected on the onclick event of the checkbox. As follows :

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AlertWithCheckBoxActivity extends Activity {
    /** Called when the activity is first created. */
    static int count = 0;
    final CharSequence[] items={".NET","J2EE","PHP"};
    boolean[] itemsChecked = new boolean[items.length];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    public void showDialog(View v)
    {

        count = 0;
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Pick a Choice");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String selectedTech="Selected Tech - ";
                for (int i = 0; i < items.length; i++) {
                    if (itemsChecked[i]) {

                        selectedTech=selectedTech+items[i]+" ";
                        itemsChecked[i]=false;
                    }
                }
            }
        });

        builder.setMultiChoiceItems(items, new boolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked) {
                    if(count < 5) {
                        itemsChecked[which] = isChecked;
                        count++;
                    }else{
                        //Display your toast here
                    }
                }else{
                    count--;
                }
            }
        });
        builder.show();
    }
}

OTHER TIPS

The exact solution for OP is:

final boolean[] selected = new boolean[25];

builder.setMultiChoiceItems(R.array.values, selected, new DialogInterface.OnMultiChoiceClickListener() {

    int count = 0;

    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        count += isChecked ? 1 : -1;
        selected[which] = isChecked;

        if (count > 5) {
            Toast.makeText(getActivity(), "You selected too many.", Toast.LENGTH_SHORT).show();
            selected[which] = false;
            count--;
            ((AlertDialog) dialog).getListView().setItemChecked(which, false);
        }
    }

});

Instead of LunaVulpo's solution I used

if (((AlertDialog) dialog).getListView().getCheckedItemCount() > 2) {
...
}

Because the count resets, while the checked items still remain, when the user opens the Alertdialog again to edit his choices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top