Skip to main content

How to implement a searchable Spinner in Kotlin Android

How to implement a searchable Spinner in Kotlin Android.

Here's a step-by-step tutorial on how to implement a searchable spinner in Kotlin for Android:

  1. Create a new Android project in Kotlin.

  2. Open your project's build.gradle file and add the following dependency in the dependencies section:

    implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'

    This library provides the necessary classes and methods to create a searchable spinner.

  3. Open the layout file (activity_main.xml) where you want to add the searchable spinner. Add the following code to define the spinner:

    <com.toptoche.searchablespinnerlibrary.SearchableSpinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Select an item"
    android:padding="10dp" />

    This creates a SearchableSpinner widget with an id of spinner. You can customize the layout according to your needs.

  4. Open the Kotlin activity file (MainActivity.kt) and add the following code to initialize the spinner and populate it with data:

    import android.os.Bundle
    import android.widget.ArrayAdapter
    import androidx.appcompat.app.AppCompatActivity
    import com.toptoche.searchablespinnerlibrary.SearchableSpinner

    class MainActivity : AppCompatActivity() {

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

    val spinner: SearchableSpinner = findViewById(R.id.spinner)

    val items = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
    val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, items)
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinner.adapter = adapter
    }
    }

    In this code, we first import the necessary classes. Then, in the onCreate method, we find the spinner view by its id. Next, we create a list of items that we want to show in the spinner. We create an ArrayAdapter and set it as the adapter for the spinner.

  5. Now, when you run the app, you will see the spinner with the list of items. You can select an item by clicking on it.

  6. To enable search functionality in the spinner, add the following code after setting the adapter:

    spinner.setTitle("Select an item")
    spinner.setPositiveButton("OK")
    spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    // Handle item selection
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
    // Handle no selection
    }
    })

    This code sets the title text and positive button text for the search dialog. It also sets an OnItemSelectedListener to handle item selection and no selection events.

  7. To perform a specific action when an item is selected, add your code inside the onItemSelected method.

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    val selectedItem = parent?.getItemAtPosition(position).toString()
    // Perform action with the selected item
    }

    In this example, we retrieve the selected item using the getItemAtPosition method and convert it to a string. You can replace the comment with your desired action.

That's it! You have successfully implemented a searchable spinner in Kotlin for Android. You can now run the app and test the functionality.