How to set selected item in spinner in android on button click programmatically

Dynamically set user choice through button click into spinner manually.

In this tutorial we are dynamically setting up spinner values on button click. So app user can only click on button and it will automatically setup its choice inside spinner. So here is the complete step by step tutorial for How to set selected item in spinner in android on button click programmatically.

android-project-download-code-button

How to set selected item in spinner in android on button click programmatically.

Code for MainActivity.java file.

 package com.setselecteditemspinner_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class MainActivity extends Activity {

 Spinner spinnerTest;
 Button setItem;
 
 String[] mobile = new String[]{
 
 "MICROMAX",
 "SAMSUNG",
 "SONY",
 "GIONEE",
 "LENOVO"
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinnerTest = (Spinner)findViewById(R.id.spinner1);
 
 setItem = (Button)findViewById(R.id.button1);
 
 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
 this,R.layout.textview_for_spinner,mobile );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_for_spinner);
 
 spinnerTest.setAdapter(spinnerArrayAdapter);
 
 setItem.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
// Here  1 means the second item of spinner because array adapter indexing starts
// with 0(zero).
 
 spinnerTest.setSelection(1);
 }
 });
 
 }
 }

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.setselecteditemspinner_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" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:text="Click here to set SAMSUNG in spinner on button click programmatically" />

</RelativeLayout>

Code for textview_for_spinner.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="#000402"
 android:padding="10dp" />

Screenshot:

How to set selected item in spinner in android on button click programmatically

Click here to download How to set selected item in spinner in android on button click programmatically project.