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

Popular posts from this blog

How to call Post API using Retrofit in Android using Jetpack Compose?

Note: If you are seeking Java code for Jetpack Compose , please note that Jetpack Compose is only available in Kotlin . It uses features such as coroutines , and the handling of @Composable annotations is handled by a Kotlin compiler. There is no method for Java to access these. Therefore, you cannot use Jetpack Compose if your project does not support Kotlin. Step By Step Implementation Step 1: Create a New Project in Android Studio We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project. Step 2: Add the below dependency to your build.gradle File Navigate to the Gradle Scripts > build.gradle (Module:app) and add the below dependency in the dependencies section.  // below dependency for using the retrofit implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.5.0’ After adding this dependency sync your project and now move towards the AndroidManifes...

What is the Android Architecture?

 Well, Android Architecture is categorized into five part.     1. Linux kernel     2. Native Libraries (Middleware),     3. Android Runtime     4. Application Framework     5. Applications You can see the picture shown below. Linux kernel: It is the heart of android architecture that exists at the root of android architecture. Linux kernel is responsible for device drivers, power management, memory management, device management and resource access. Native Libraries: On the top of Linux kernel, their are Native libraries such as Web-Kit, Open-GL, Free-Type, SQLite, Media, C runtime library (libc) etc. The Web-Kit library is responsible for browser support, SQLite is for database, Free-Type for font support, Media for playing and recording audio and video formats. Android Runtime: In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is responsible to run android application. DVM...