Igor Filippov Igor Filippov - 1 year ago 210
Android Question

Android Spinner with multiple choice

How do I create spinner which allows to choose multiple items, i.e spinner with check boxes?

Answer Source

I have written custom implementation of MultiSpinner. It's looking similar to normal spinner, but it has checkboxes instead of radiobuttons. Selected values are displayed on the spinner divided by comma. All values are checked by default. Try it:

package cz.destil.settleup.gui;

public class MultiSpinner extends Spinner implements
        OnMultiChoiceClickListener, OnCancelListener {

    private List<String> items;
    private boolean[] selected;
    private String defaultText;
    private MultiSpinnerListener listener;

    public MultiSpinner(Context context) {

    public MultiSpinner(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);

    public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);

    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        if (isChecked)
            selected[which] = true;
            selected[which] = false;

    public void onCancel(DialogInterface dialog) {
        // refresh text on spinner
        StringBuffer spinnerBuffer = new StringBuffer();
        boolean someUnselected = false;
        for (int i = 0; i < items.size(); i++) {
            if (selected[i] == true) {
                spinnerBuffer.append(", ");
            } else {
                someUnselected = true;
        String spinnerText;
        if (someUnselected) {
            spinnerText = spinnerBuffer.toString();
            if (spinnerText.length() > 2)
                spinnerText = spinnerText.substring(0, spinnerText.length() - 2);
        } else {
            spinnerText = defaultText;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                new String[] { spinnerText });

    public boolean performClick() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                items.toArray(new CharSequence[items.size()]), selected, this);
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
        return true;

    public void setItems(List<String> items, String allText,
            MultiSpinnerListener listener) {
        this.items = items;
        this.defaultText = allText;
        this.listener = listener;

        // all selected by default
        selected = new boolean[items.size()];
        for (int i = 0; i < selected.length; i++)
            selected[i] = true;

        // all text on the spinner
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_item, new String[] { allText });

    public interface MultiSpinnerListener {
        public void onItemsSelected(boolean[] selected);

You use it in XML like this:

<cz.destil.settleup.gui.MultiSpinner android:id="@+id/multi_spinner" />

And you pass data to it in Java like this:

MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner);
multiSpinner.setItems(items, getString(R.string.for_all), this);

Also you need to implement the listener,which will return the same length array , with true or false to show selected to unselected..

public void onItemsSelected(boolean[] selected);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download