How to get selected item position of spinner in android

How to set spinner clicked items string array position in number into TextView like one, two, three using getSelectedItemPosition() method.

Spinner item position means the string array position on spinner element because every string array starts with index zero( 0 ) then one( 1)…..(n). So by getting spinner item position we can perform various type of tasks upon it. So here is the complete step by step tutorial for How to get selected item position of spinner in android.

android-project-download-code-button

How to get selected item position of spinner in android.

Code for MainActivity.java file.

 package com.getselecteditempositionspinner_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

public class MainActivity extends Activity {

 Spinner SpinnerItemPosition;
 TextView PositionDisplay;
 int Hold;
 
 String[] number = new String[]{
 "WORDPRESS",
 "THEMES",
 "PLUGIN",
 "LAYOUT",
 "QUESTIONS"
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SpinnerItemPosition = (Spinner)findViewById(R.id.spinner1);
 PositionDisplay = (TextView)findViewById(R.id.textView1);
 
 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
 this,R.layout.spinner_item_position,number );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item_position);
 
 SpinnerItemPosition.setAdapter(spinnerArrayAdapter);
 
 SpinnerItemPosition.setOnItemSelectedListener(new OnItemSelectedListener() {

 @Override
 public void onItemSelected(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 Hold = SpinnerItemPosition.getSelectedItemPosition() + 1 ;
 
 PositionDisplay.setText("Item Position is = " + Hold );
 
 }

 @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.getselecteditempositionspinner_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/spinner1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="19dp"
 android:text="Spinner position"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Code for spinner_item_position.xml file.

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:textColor="#7d462e"
 android:padding="12dp" />

Screenshot:

How to get selected item position of spinner in android

spinner-position-new

Click here to download How to get selected item position of spinner in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *