MainActivity.kt
package com.cfsuman.kotlintutorials
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
class MainActivity : AppCompatActivity() {
private lateinit var context:MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the context
context = this
// Get the widgets reference from XML layout
val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)
val spinner = findViewById<Spinner>(R.id.spinner)
val textView = findViewById<TextView>(R.id.textView)
// list of spinner items
val list = mutableListOf(
"Amaranth purple",
"Black olive",
"Asparagus",
"Android green",
"Blue sapphire"
)
// add a hint to spinner
// list first item will show as hint
list.add(0,"Select favorite color")
// initialize an array adapter for spinner
val adapter:ArrayAdapter<String> = object: ArrayAdapter<String>(
context,
android.R.layout.simple_spinner_dropdown_item,
list
){
override fun getDropDownView(
position: Int,
convertView: View?,
parent: ViewGroup
): View {
val view:TextView = super.getDropDownView(
position,
convertView,
parent
) as TextView
// set item text bold
view.setTypeface(view.typeface, Typeface.BOLD)
// set item padding
view.setPadding(64,0,128,0)
// set selected item style
if (position == spinner.selectedItemPosition
&& position !=0 ){
view.background = ColorDrawable(
Color.parseColor("#F8F8F8"))
view.setTextColor(
Color.parseColor("#0018A8"))
}
// make hint item color gray
if(position == 0){
view.setTextColor(Color.LTGRAY)
}
return view
}
override fun isEnabled(position: Int): Boolean {
// disable first item
// first item is display as hint
return position != 0
}
}
// finally, data bind spinner with adapter
spinner.adapter = adapter
// spinner on item selected listener
spinner.onItemSelectedListener = object
: AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
// by default spinner initial selected item is first item
if (position != 0){
textView.text = "Selected : "
// get selected item text
textView.append(
parent.getItemAtPosition(position).toString()
)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
// another interface callback
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#DCDCDC"
android:padding="24dp">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF2h8nwvty8_6YOIjQKyGxBxq6BLC1_5ZcngnBG4FA0Yew7tztdAnALGDABlZeZMmYc-BtQfAq6bfnvcb0c5ctD-zgJSwirrJHEhR0P6J5gLVZG8EBbHqSPDRHAbDZ_ocad_rXxh0yQ6kLxu81bCOJhHcjFxHGN69XPzSLqKTZL5BTh3rD_E8Na9wzxQ/s1600/android_kotlin_add_a_hint_to_spinner_programmatically.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwMWRAf3H0vDPifWCvIH4BtViToqzQElHd85lVdsHekylE4aSeUm83zsVGcXGXhyIkqyDp6iuQUc-RDCdtIudK2HE8EbgI3sFdk1wyPZ7uSFtr991CyLT5_7uB8Du8g-6nFvwbl1Bk_plOBlUhdaorPQuPcyhgLfmOqz0RAixs_h9dQXGj7cR3gDp2_w/s1600/android_kotlin_add_a_hint_to_spinner_programmatically2.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Sp9k0XmwJXk-l-KdVmQYC_H0zCsGmNVJnl55mUd16aY7jzb4OB196zbDoHx56HhKjKZFlLDRnSKXoO_z2QrpNEwhrEDvfZdP30MnNX2eX7Sqb-4DIF2MuIiap8oVLJlVRlBSDJ2I-l27PKy629OLkudGpoFEE06uftHhvZMJ7pQPL7Icw0hAwufq4Q/s1600/android_kotlin_add_a_hint_to_spinner_programmatically3.png)