Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner with checkbox items, is it possible?

Spinner with checkbox items, is it possible?

like image 715
Jan S. Avatar asked Apr 21 '10 11:04

Jan S.


4 Answers

Try this

 <selva.spinner.multispinner android:id="@+id/multi_spinner" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

Spinner1Activity.java

    package selva.spinner;

    import java.util.ArrayList;
    import java.util.List;
    import selva.spinner.multispinner.multispinnerListener;
    import android.app.Activity;
    import android.os.Bundle;

    public class Spinner1Activity extends Activity  implements multispinnerListener
    {

     @Override
     public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    multispinner ms = (multispinner) findViewById(R.id.multi_spinner);
    List<String> list = new ArrayList<String>();
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");
    list.add("five");
    list.add("six");
    list.add("seven");
    list.add("eight");
    list.add("nine");
    list.add("ten");
    ms.setItems(list, "select", this);

  }

@Override
public void onItemschecked(boolean[] checked)
{
    // TODO Auto-generated method stub

}
}

multispinner.java

 package selva.spinner;

 import java.util.List;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnCancelListener;
 import android.content.DialogInterface.OnMultiChoiceClickListener;
 import android.util.AttributeSet;
 import android.widget.ArrayAdapter;
 import android.widget.Spinner;


public class multispinner extends Spinner implements
OnMultiChoiceClickListener, OnCancelListener 
{
      private List<String> listitems;
      private boolean[] checked;

      public multispinner(Context context) 
      {
          super(context);
      }

      public multispinner(Context arg0, AttributeSet arg1)
      {
          super(arg0, arg1);
      }

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

      @Override
      public void onClick(DialogInterface dialog, int ans, boolean isChecked)
      {
          if (isChecked)
              checked[ans] = true;
          else
              checked[ans] = false;
      }


    @Override
      public void onCancel(DialogInterface dialog)
      {

        String str="Selected values are: ";

            for (int i = 0; i < listitems.size(); i++)
            {
                        if (checked[i] == true)
                        {
                        str=str+"   "+listitems.get(i);
                        }

            }

                AlertDialog.Builder alert1 = new AlertDialog.Builder(getContext());

                alert1.setTitle("Items:");

                alert1.setMessage(str);

                alert1.setPositiveButton("Ok", null);

                alert1.show();


      }

      @Override
      public boolean performClick()
      {
          AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
          builder.setMultiChoiceItems(
                    listitems.toArray(new CharSequence[listitems.size()]), checked, this);
          builder.setPositiveButton("done",
                  new DialogInterface.OnClickListener()
          {

              @Override
              public void onClick(DialogInterface dialog, int which)
              {
                  dialog.cancel();
              }
          });
          builder.setOnCancelListener(this);
          builder.show();
          return true;
      }

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

          checked = new boolean[items.size()];
          for (int i = 0; i < checked.length; i++)
              checked[i] =false;


          ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                  android.R.layout.simple_spinner_item, new String[] { allText });
          setAdapter(adapter);
        }

      public interface multispinnerListener 
      {
          public void onItemschecked(boolean[] checked);
      }

 }
like image 56
selva_pollachi Avatar answered Oct 05 '22 23:10

selva_pollachi


You can use the multiSpinner:

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

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) {
    super(context);
}

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

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

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

@Override
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(items.get(i));
            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(),
            android.R.layout.simple_spinner_item,
            new String[] { spinnerText });
    setAdapter(adapter);
    listener.onItemsSelected(selected);
}

@Override
public boolean performClick() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMultiChoiceItems(
            items.toArray(new CharSequence[items.size()]), selected, this);
    builder.setPositiveButton(R.string.ok,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    builder.setOnCancelListener(this);
    builder.show();
    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 });
    setAdapter(adapter);
}

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

}

And then in your layout .xml:

<xxx.xx.gui.MultiSpinner android:id="@+id/SpinnerCollegues"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:prompt="@string/university"/>
like image 22
chemalarrea Avatar answered Oct 06 '22 01:10

chemalarrea


There is implemented a MultiSpinner, you can find it on AndroidArsenal

Can find it on Maven Repository

If you add a hint to it, looks nice: android:hint="Choose ..."

like image 37
Arnold Vakaria Avatar answered Oct 05 '22 23:10

Arnold Vakaria


That depends on what you mean.

If you want a true multi-select Spinner, then there's nothing built into Android for that.

Note that you are in control over what goes in the Spinner rows of the drop-down list, except for the radio button. If you want to put checkboxes in your rows, be my guest. It'll look strange, may not work properly with respect to touch events, will not remove the radio buttons (AFAIK), and will be completely unrelated to the Spinner's contents in normal mode. Hence, I can't recommend this approach, but it is doable.

The source code to Spinner is available from the Android open source project, so you are welcome to clone it and develop a MultiSelectSpinner or something.

like image 42
CommonsWare Avatar answered Oct 06 '22 00:10

CommonsWare