Skip to main content

How to make a 'SET SELECTION' in spinner if you have a model type array list data in android kotlin?

 //if a have a value like 'Item 3' need to set selection

  val spinnerItemValue="Item 3"//some country name


//find the index of the item with the target text

val position = arrayList!!.indexOfFirst { it.countryName(//Note: model key which store the country   name for spinner dorp-down)==spinnerItemValue}

 //set the selection in the spinner

 if(position>=0){

      //if you user data binding in your project

       binding.countrySpinner.setSelection(position)

    //if you use findViewById() in your project

      Spinner countrySpinner=findViewById(R.id.countrySpinner)

      countrySpinner.setSelection(position)

}

Comments