activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spin"
android:background="#fff"></Spinner>
</LinearLayout>
MainActivity.kt
package com.androinidan.spinner
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Spinner
import android.widget.Toast
class MainActivity : AppCompatActivity() {
var studentdata= arrayOf("Ashih","Swathi","Anuj","Raj", "Ashih","Swathi","Anuj","Raj",
"Ashih","Swathi","Anuj","Raj", "Ashih","Swathi","Anuj","Raj","Ashih","Swathi","Anuj","Raj","451536234")
var spinner: Spinner?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner=findViewById(R.id.spin)
var adp: ArrayAdapter<*>
adp= ArrayAdapter(applicationContext,android.R.layout.simple_list_item_1,studentdata)
spinner!!.adapter=adp
spinner!!.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>,
view: View, position: Int, id: Long) {
Toast.makeText(this@MainActivity,
" "+position, Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
}
}
}