Android Spinner with Multiple Choice

Android Spinner with multiple choice

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) {
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 someSelected = false;
for (int i = 0; i < items.size(); i++) {
if (selected[i] == true) {
spinnerBuffer.append(items.get(i));
spinnerBuffer.append(", ");
someSelected = true;
}
}
String spinnerText;
if (someSelected) {
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(android.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);
}
}

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);

Android Spinner With Multiple Selection

ohhh, The small and miner mistake you've done is you forgot to implement the interface MultiSpinnerListener. Your DCOReader Activity should be like this,

public class DCOReader extends AppCompatActivity implements MultiSpinner.MultiSpinnerListener{
....
}

How to use Android Spinner with multiple choice in Fragment of Navigation drawer activity


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

Try using multiselectionspinner.
https://stackoverflow.com/a/6022474/6616489

And for

multiple choice spinner inside the fragment of Navigation drawer

Refer https://stackoverflow.com/a/20017862/6616489

Multi selection in spinner

By default Spinner control is in Single Choice selection mode.

What you done is you just changed the Layout to show the spinner item with Checkbox. which doesn't mean you are showing an muti selection spinner control.

You can refer any below mentioned sample to implement multi select spinner.

multi-select-drop-down-list

android-spinner-like-ui-for-selecting-multiple-options

spinner-with-multiple-selectiondescription here

Multiple Selection Spinner

S spinner is designed to show one item at a time. You might get away by making a spinner of checkboxes, but it will be probably an awful user experience.

I would suggest you a ListView instead and CHOICE_MODE_MULTIPLE. You can add a listview to a dialog as shown in this answer: is it possible to create listview inside dialog?



Related Topics



Leave a reply



Submit