Search code examples
androidtextkotlinspinner

kotlin android spinner text color


I created a spinner, but I would like to change the text colour and the font size. There are a lot of tutorial on the web in Java. I have difficulty in translate Java to Kotlin because I am new. Please help to find the solution.

enter image description here

SpinnerActivity.kt

class SpinnerActivity : AppCompatActivity() {

    lateinit var option : Spinner
    lateinit var result :TextView

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

        option = findViewById(R.id.sp_option) as Spinner
        result = findViewById(R.id.tv_result) as TextView

        val options = arrayOf("111", "222", "333")

        option.adapter = ArrayAdapter<String>(this,R.layout.spinner_layout,options)


        option.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

                result.text = "please select an option"
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

                result.text = options.get(position)
            }
        }

activity_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SpinnerActivity">

    <Spinner
        android:id="@+id/sp_option"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light"
        />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>

</LinearLayout>

spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView
        android:id="@+id/custom_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        />
</RelativeLayout>

Solution

  • Create one xml for your required textview like following.

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="24sp" />
    

    Name it as item.xml and change following in your activity class.

    option.adapter = ArrayAdapter<String>(this, R.layout.item, options)
    

    That's it!!!