Studyon Minte9.com
Android




Spinners



Spinners provide a quick way to select one value from a set.


1) Populate the spinner


	%--- activity_main.layout

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content">
	    
	    <!-- LOOK HERE -->

	    <Spinner 
		android:id="@+id/planets_spinner"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		/>
	    
	</LinearLayout>
	

	%--- strings.xml

	<resources>

	    <string name="app_name">MyApp</string>
	    <string name="action_settings">Settings</string>
	    
	    <!-- LOOK HERE -->

	    <string-array name="planets_array">
		<item>Mercury</item>
		<item>Venus</item>
		<item>Earth</item>
	    </string-array>
	    
	</resources>


	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.widget.ArrayAdapter;
	import android.widget.Spinner;

	public class MainActivity extends Activity {
		     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		/*--- LOOK HERE ---*/

		Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
		
		ArrayAdapter<CharSequence> adapter = 
				ArrayAdapter.createFromResource(this, R.array.planets_array, 
                                            android.R.layout.simple_spinner_item);
		
		// layout to choose when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spinner.setAdapter(adapter);
	    }
	}



2) Responding to User Selections

When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event.


	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.widget.ArrayAdapter;
	import android.widget.Spinner;

	public class MainActivity extends Activity {
			     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		setSpinner();
	    }
	    
	    public void setSpinner() {
		Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
		
		ArrayAdapter<CharSequence> adapter = 
				ArrayAdapter.createFromResource(this, R.array.planets_array, 
                                        android.R.layout.simple_spinner_item);
		
		// layout to choose when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spinner.setAdapter(adapter);
		
		/*--- LOOK HERE --*/
		
		spinner.setOnItemSelectedListener(new SpinnerActivity());
	    }
	}


	%--- SpinnerActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.view.View;
	import android.widget.AdapterView;
	import android.widget.AdapterView.OnItemSelectedListener;
	import android.widget.Toast;

	/*-- LOOK HERE --*/

	public class SpinnerActivity extends Activity implements OnItemSelectedListener {
		
		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int pos, long id){
			
			String message = parent.getItemAtPosition(pos).toString();
			
			if (!message.equals("Choose")) {
				Toast.makeText(parent.getContext(), message, Toast.LENGTH_SHORT).show();
			}
		}
		
		@Override
		public void onNothingSelected(AdapterView<?> parent) {}

	}


http://developer.android.com/guide/topics/ui/controls/spinner.html
http://www.mkyong.com/android/android-spinner-drop-down-list-example