Download Code

Android Spinner Example

In android spinner example you will learn how to create spinners example in android application.Let me give you some information about Spinners.

Spinners

Provide a quick way to select one value from a set or you can say drop down list. In the default state, a spinners view shows its currently selected value. On touching displays a drop down menu with all other available values, from which the user can select a new one.For More detail Link.

You will create two kind of Spinners view.

  • Using XML layout
  • Programmatically

Array Adapter

A concrete BaseAdapter that is backed by an array of arbitrary objects.Link.

Alright let’s begin.Create new android spinner example application project.

Go to activity_main.xml.Add two TextViews and two Spinners in layout.

android spinner,android spinner example,spinner example,spinner,

Next go to your values > strings.xml file.Add array in resources.

android spinner,android spinner example,spinner example,spinner,

Go to your MainActivity.java class.Do the following steps.

  • Create spinners variable.
  • Refer that spinners views.
  • Create ArrayAdapter,pass array and spinners layout style.
  • Set Drop Down View of Spinners.
  • Set adapter to spinners.

Now for Programmatic Spinners.Do the following steps.

  • Create spinners variable.
  • Refer that spinners.
  • Create ArrayAdapter,pass ListArray and spinner layout style.
  • Set Drop Down View of Spinners.
  • Set adapter to spinners.

Complete MainActivity.java.

package developer.codecspinners;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;



public class MainActivity extends Activity {

	Spinner staticSpin,dynamicSpin;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialize();
		// Create an ArrayAdapter using the string array and a default spinners
		ArrayAdapter adapt = ArrayAdapter.createFromResource(this, R.array.expression, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapt
		.setDropDownViewResource(android.R.layout.simple_list_item_checked);

		// Apply the adapter to the spinners
		staticSpin.setAdapter(adapt);
		
		List personName = new ArrayList();

		personName.add("Aslam");
		personName.add("Ali");
		personName.add("Ayyan");
		personName.add("Sabir");
		personName.add("Nuashad");
		personName.add("Aryan");
		personName.add("Salman");
		personName.add("Allen");
		personName.add("Zain");
		personName.add("Inestia");
		personName.add("Bilal");
		personName.add("Aliya");
		
		ArrayAdapter dynamicAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, personName);
		dynamicAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);

		dynamicSpin.setAdapter(dynamicAdapter);	
	}
	public void initialize(){
		staticSpin = (Spinner) findViewById(R.id.spinner1);
		dynamicSpin = (Spinner) findViewById(R.id.spinner2);
	}
}

Now run your android spinners example application project.

android spinner,android spinner example,spinner example,spinner,
That’s it for this tutorial.You have used checked spinner selection method.You can also use other liek radio etc.
Thanks.