6

I am using below code to create a simple Spinner (only single selection is allow)

But now i would like to know how can i use spinner to make multiple selection, I know i can achieve this using Dialog but i have to use Spinner..

public class MainActivity extends Activity {

    Spinner spnr;

    String[] celebrities = {
            "Chris Hemsworth",
            "Jennifer Lawrence",
            "Jessica Alba",
            "Brad Pitt",
            "Tom Cruise",
            "Johnny Depp",
            "Megan Fox",
            "Paul Walker",
            "Vin Diesel"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spnr = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, celebrities);

        spnr.setAdapter(adapter);
        spnr.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {

                        int position = spnr.getSelectedItemPosition();
                        Toast.makeText(getApplicationContext(),"You have selected "+celebrities[+position],Toast.LENGTH_LONG).show();
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }

                }
            );
    }

}
Sophie
  • 2,524
  • 10
  • 40
  • 73

2 Answers2

6

Spinner by default cannot work for Multiple selection.

If you need it, you have to extend it, check here

Android Spinner with multiple choice

Community
  • 1
  • 1
Derek Fung
  • 8,113
  • 1
  • 23
  • 28
1

To create a spinner but with MultiSelect dropdown functionality, you can use this library in android MultiSelectSpinner

You can see readme file to know how to use this library. OR, Here is the simple example showing how to implement this library

  • add dependency in app level build.gradle. You can click above library link for current version. For now I'm using 1.0.0
    implementation 'com.github.puskal-khadka:MultiSelectSpinner:1.0.0'
  • Add following view on xml
<com.puskal.multiselectspinner.MultiSelectSpinnerView
     android:id="@+id/multiSelectSpinner"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginEnd="8dp"
  />
  • Finally setup data, logic for spinner on activity/fragment
  val testDataList = arrayListOf("Kotlin", "Java", "Python", "Php", "Swift")
        with(binding) {
            multiSelectSpinner.buildCheckedSpinner(testDataList){ selectedPositionList, displayString ->
                tvSelectedPosition.text = "Selected position:  $selectedPositionList" //if kotlin, python selecteed:returned postion will be 0,2
                tvDispString.text = "Display String:  $displayString"
            }
        }
puskal Khadka
  • 101
  • 2
  • 4