Skip to main content

How to change the text color of selected item in a Spinner in Kotlin Android

How to change the text color of selected item in a Spinner in Kotlin Android.

Here's a step-by-step tutorial on how to change the text color of the selected item in a Spinner in Kotlin for Android.

Step 1: Create a new Android project

Start by creating a new Android project in Android Studio. Choose an appropriate project name and set the minimum SDK version according to your requirements.

Step 2: Add a Spinner to your layout XML file

In your layout XML file (e.g., activity_main.xml), add a Spinner element. You can customize its appearance and properties as per your needs. Here's an example:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/spinner_items" />

Step 3: Define an array of spinner items

In your strings.xml file, define an array of items that will be displayed in the Spinner. You can add your desired items to the array. Here's an example:

<string-array name="spinner_items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>

Step 4: Create a custom ArrayAdapter

In your Kotlin file (e.g., MainActivity.kt), create a custom ArrayAdapter class that extends the ArrayAdapter class. Override the getView method to customize the appearance of the Spinner items. Here's an example:

class CustomArrayAdapter(
context: Context,
resource: Int,
objects: Array<String>
) : ArrayAdapter<String>(context, resource, objects) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val textView = view.findViewById<TextView>(android.R.id.text1)
textView.setTextColor(Color.RED) // Change the text color here
return view
}
}

Step 5: Initialize and set the custom ArrayAdapter to the Spinner In your MainActivity.kt file, initialize the Spinner and set the custom ArrayAdapter to it. Here's an example:

val spinner = findViewById<Spinner>(R.id.spinner)
val spinnerItems = resources.getStringArray(R.array.spinner_items)
val adapter = CustomArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerItems)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter

Step 6: Change the text color of the selected item To change the text color of the selected item, you need to handle the Spinner's item selection event. Add an OnItemSelectedListener to the Spinner and override its onItemSelected method. Inside this method, you can change the text color of the selected item. Here's an example:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View?,
position: Int,
id: Long
) {
val selectedItem = parent.getChildAt(0) as TextView
selectedItem.setTextColor(Color.BLUE) // Change the text color here
}

override fun onNothingSelected(parent: AdapterView<*>) {
// Handle nothing selected case, if needed
}
}

That's it! Now, when you run your application, the selected item in the Spinner will have the specified text color. Feel free to customize the text color values according to your preference.

I hope this tutorial helps you change the text color of the selected item in a Spinner in Kotlin for Android!