change spinner text color android kotlin
To change the spinner text color in Android using Kotlin, follow these steps:
Create a new XML file in the
res
folder of your project. Name itspinner_item.xml
(or any name of your choice).In the
spinner_item.xml
file, add the following code:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:textColor="@color/your_color_here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp" />
Replace @color/your_color_here
with the color value you want to use for the spinner text.
- In your Kotlin activity or fragment, find the spinner view using its ID and set a custom adapter for it. For example:
val spinner = findViewById<Spinner>(R.id.your_spinner_id)
val adapter = ArrayAdapter.createFromResource(this, R.array.your_array_id, R.layout.spinner_item)
spinner.adapter = adapter
Replace R.id.your_spinner_id
with the ID of your spinner view, R.array.your_array_id
with the ID of your string array resource (if using one), and R.layout.spinner_item
with the name of the XML file you created in step 1.
And that's it! The spinner text color should now be changed to the color you specified in the spinner_item.xml
file.