Get selected item value from Spinner in Android example

Retrieve Spinner drop down list selected item value in android.

Spinner is a type of drop down list menu and you can select your item by tapping on mobile screen. OnItemSelectedListener() function is apply to get selected item value from spinner drop down list menu. So here is the complete tutorial for Get selected item value from Spinner in Android example.

android-project-download-code-button

Get selected item value from Spinner in Android example.

Code for MainActivity.java file.

 package com.android_examples.com.getselecteditemspinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

 Spinner spinnerDropDownView;
 String[] spinnerValueHoldValue = {"PHP", "ANDROID", "WEB-DESIGN", "PHOTOSHOP"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 spinnerDropDownView =(Spinner)findViewById(R.id.spinner1);
 ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, spinnerValueHoldValue);
 spinnerDropDownView.setAdapter(adapter);
 
 spinnerDropDownView.setOnItemSelectedListener(new OnItemSelectedListener() {

 @Override
 public void onItemSelected(AdapterView<?> parent, View view,
 int position, long id) {
 
 Toast.makeText(MainActivity.this, spinnerDropDownView.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
 
 }

 @Override
 public void onNothingSelected(AdapterView<?> parent) {
 // TODO Auto-generated method stub
 
 }
 });
}
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.getselecteditemspinner.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="60dp" />

</RelativeLayout>

Screenshot:

Get selected item value from Spinner in Android example

Click Here to Download Get selected item value from Spinner in Android example project.