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:
Create a new Android project in Kotlin.
Open your project's
build.gradlefile and add the following dependency in thedependenciessection:implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'This library provides the necessary classes and methods to create a searchable spinner.
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
SearchableSpinnerwidget with an id ofspinner. You can customize the layout according to your needs.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
onCreatemethod, 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 anArrayAdapterand set it as the adapter for the spinner.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.
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
OnItemSelectedListenerto handle item selection and no selection events.To perform a specific action when an item is selected, add your code inside the
onItemSelectedmethod.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
getItemAtPositionmethod 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.