In android, when you want to show multiple options, then spinner is the better option to display the data. It will display the multiple options to the user from which the user can select an option. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
In this article, we will see how can we get the selected item position and value in the spinner. Unless you get the value of selected value in the spinner, you won’t be able to perform any action based on the selected value. Below are the steps to get the selected value of spinner in android.
Table of Contents
Create the XML layout for the spinner
Let us first create an XML layout that will have values in the spinner. Below is the code for same.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="50dp"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Spinner Position "
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Spinner value"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Now create the java file.
- parent.getSelectedItemPosition(); is used to get the selected item position.
- String.valueOf(parent.getItemAtPosition(position)); is used to get the value of selected item value from Spinner
Below is the Java Code
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String[] categorydrop = {"Select Category", "Small", "Mid Size", "Export Orinted"};
Spinner spinner;
TextView textView1, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, categorydrop);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int getid = parent.getSelectedItemPosition(); ///get the selected element place id
textView1.setText("Position of selected element: "+String.valueOf(getid));
String getvalue = String.valueOf(parent.getItemAtPosition(position)); // getting the selected element value
textView2.setText("Value of Selected Spinner : "+getvalue);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
You are all set now, using the code above, you can get the value and position of the selected item in Spinner.