How to Set the Selected Item of Spinner By Value and Not By Position in Android?
Spinner is used in many android applications to display multiple options within a drop-down list and the user will be able to select a specific option from the given list. The default item which is selected within the spinner is the first item within the list which we will be creating to be displayed within the spinner. We can set the specific item selected within the spinner with the help of the position of that item within the list. In this article, we will take a look at How to set the selected item of the spinner by value and not by position.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. The code has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with activity_main.xml
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!-- on below line we are creating a text for heading of our app --> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/idLanguageSpinner" android:layout_margin = "20dp" android:gravity = "center" android:padding = "4dp" android:text = "Spinner in Android" android:textAlignment = "center" android:textColor = "@color/purple_200" android:textSize = "20sp" android:textStyle = "bold" /> <!-- on below line we are creating a spinner --> < Spinner android:id = "@+id/idLanguageSpinner" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_margin = "10dp" android:padding = "10dp" /> </ RelativeLayout > |
Step 3: Working with MainActivity File
Navigate to app > java > your app’s package name > MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
import android.os.Bundle import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Spinner import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener { // on below line we are creating variable for spinner. lateinit var languageSpinner: Spinner // on below line we are creating a variable for our list of data to be displayed in spinner. var languages = arrayOf<String>( "C++" , "Java" , "Kotlin" , "JavaScript" , "Python" , "PHP" , "C#" , "C" ) override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing spinner with ids. languageSpinner = findViewById(R.id.idLanguageSpinner) // on below line we are adding click listener for our spinner languageSpinner.onItemSelectedListener = this // on below line we are initializing adapter for our spinner val adapter: ArrayAdapter<CharSequence> = ArrayAdapter( this , android.R.layout.simple_spinner_item, languages) // on below line we are setting drop down view resource for our adapter. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) // on below line we are setting adapter for spinner. languageSpinner.adapter = adapter // on below line we are creating a variable to which we have to set our spinner item selected. val selection = "Python" // on below line we are getting the position of the item by the item name in our adapter. val spinnerPosition: Int = adapter.getPosition(selection) // on below line we are setting selection for our spinner to spinner position. languageSpinner.setSelection(spinnerPosition) } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { // on below line we are displaying toast message for selected item. Toast.makeText( this , "" + languages.get(position) + " Selected.." , Toast.LENGTH_SHORT) .show() } override fun onNothingSelected(parent: AdapterView<*>?) { } } |
Java
import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Arrays; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { // on below line we are creating variable for spinner. Spinner languageSpinner; // on below line we are creating a variable for our list of data to be displayed in spinner. String[] languages = { "C++" , "Java" , "Kotlin" , "JavaScript" , "Python" , "PHP" , "C#" , "C" }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line we are initializing spinner with ids. languageSpinner = (Spinner) findViewById(R.id.idLanguageSpinner); // on below line we are initializing adapter for our spinner ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>( this , android.R.layout.simple_spinner_item, languages); // on below line we are setting drop down view resource for our adapter. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // on below line we are setting adapter for spinner. languageSpinner.setAdapter(adapter); // on below line we are adding click listener for our spinner languageSpinner.setOnItemSelectedListener( this ); // on below line we are creating a variable to which we have to set our spinner item selected. String selection = "Python" ; // on below line we are getting the position of the item by the item name in our adapter. int spinnerPosition = adapter.getPosition(selection); // on below line we are setting selection for our spinner to spinner position. languageSpinner.setSelection(spinnerPosition); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // on below line we are displaying toast message for selected item Toast.makeText(MainActivity. this , "" + languages[position] + " Selected.." , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } } |
Now run your application to see the output of it.
Output:
Contact Us