Skip to main content

How to implement a multi-select Spinner in Kotlin Android

How to implement a multi-select Spinner in Kotlin Android.

Here's a step-by-step tutorial on how to implement a multi-select Spinner in Kotlin Android:

Step 1: Set up a new Android project

  • Open Android Studio and create a new project.
  • Choose an appropriate project name and package name.
  • Select the minimum SDK version and other project settings according to your requirements.
  • Wait for the project to be created.

Step 2: Add the necessary dependencies

  • Open the build.gradle (Module: app) file.
  • Add the following dependency in the dependencies block:
implementation 'com.jaredrummler:material-spinner:1.3.1'
  • Sync the project to download the dependency.

Step 3: Create the layout file

  • Open the activity_main.xml file in the res/layout directory.
  • Add the following code to create a Spinner and a Button:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select items"
android:padding="8dp"
android:background="@android:drawable/btn_dropdown"
android:dropDownHorizontalOffset="0dp"
android:dropDownVerticalOffset="0dp"
android:dropDownWidth="match_parent"
android:spinnerMode="dropdown"
android:theme="@style/SpinnerTheme" />

<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Submit" />

</RelativeLayout>

Step 4: Create a custom adapter

  • Create a new Kotlin file called CustomAdapter.kt.
  • Add the following code to create a custom adapter that extends ArrayAdapter:
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.CheckBox
import kotlinx.android.synthetic.main.item_spinner.view.*

class CustomAdapter(context: Context, private val itemList: List<String>) :
ArrayAdapter<String>(context, 0, itemList) {

private val selectedItems = mutableListOf<String>()

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = convertView ?: LayoutInflater.from(context)
.inflate(R.layout.item_spinner, parent, false)

val item = itemList[position]
view.checkBox.text = item
view.checkBox.isChecked = selectedItems.contains(item)

view.checkBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
selectedItems.add(item)
} else {
selectedItems.remove(item)
}
}

return view
}

fun getSelectedItems(): List<String> {
return selectedItems
}
}

Step 5: Implement the multi-select Spinner in MainActivity

  • Open the MainActivity.kt file.
  • Add the following code to implement the multi-select Spinner functionality:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.jaredrummler.materialspinner.MaterialSpinner
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

private val itemList = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val adapter = CustomAdapter(this, itemList)
spinner.setAdapter(adapter)

btn_submit.setOnClickListener {
val selectedItems = adapter.getSelectedItems()
// Use the selected items as needed
}
}
}

Step 6: Run the application

  • Connect an Android device or start an emulator.
  • Run the application and test the multi-select Spinner functionality.

That's it! You have successfully implemented a multi-select Spinner in Kotlin Android. The selected items can be retrieved by calling the getSelectedItems() method on the custom adapter.