Android Spinners(Dropdownlist) tutorial with example

 

            Android spinners are not nothing but the dropdownlist seen in other programming languages. It provides a easy way to select  value from  list. Clicking spinner will display a dropdownlist with the available values.
            Lets see this concept with an example, which is quite simple with very few lines of code.

Step 1
        -- Add a spinner object to your layout as shown below.

<Spinner
android:id="@+id/spinnerstate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />

       --  Add a text view control that displays the selected value from spinner.

Step 2
        --  Declare a string array which is going to be the source for the spinner.

private String[] state= {"Andra Pradesh","Arunachal Pradesh","Assam","Bihar","Haryana","Himachal Pradesh", "Jammu and Kashmir", "Jharkhand","Karnataka", "Kerala","Tamil Nadu"};

This source needs to be bound with the spinner through ArrayAdapter. We will be making use of the below constructor.

public ArrayAdapter (Context context, int resource, List<T> objects)

where we need to pass the current context, id, the objects to represent in the ListView - as shown below.

ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, state);

Step 3
         -- Set the layout resource to create the drop down views and bind it to spinner object
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinnerState.setAdapter(adapter_state);

          -- Set the spinner to listen to selected events.

spinnerState.setOnItemSelectedListener(this);

Step 4
        -- Once an item is selected, the below callback fucntion will get invoked and we need to perform user defined functions here. I have displayed the selected value.

public void onItemSelected(AdapterView<?> parent, View view, int position,long id)



Android Spinners


Selected value


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Spinner
android:id="@+id/osversions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />

<TextView
android:id="@+id/selVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/osversions"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp" />

</RelativeLayout>


Main_activity.java

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.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinnerOsversions;
TextView selVersion;
private String[] state = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean",
"kitkat" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(state.length);
selVersion = (TextView) findViewById(R.id.selVersion);
spinnerOsversions = (Spinner) findViewById(R.id.osversions);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, state);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerOsversions.setAdapter(adapter_state);
spinnerOsversions.setOnItemSelectedListener(this);

}

public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
spinnerOsversions.setSelection(position);
String selState = (String) spinnerOsversions.getSelectedItem();
selVersion.setText("Selected Android OS:" + selState);
}

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

}

}

Source code of this application:
AndroidSpinners




14 comments:
Write comments
  1. hey thanks for nice tutorial.
    it helps a lot.
    thanks for source code zip also...

    ReplyDelete
  2. Hi, Thanks for the tutorial, it really helped lot for me.

    ReplyDelete
  3. hi thnks..it helps a lot....it really very easy to understand..

    ReplyDelete
  4. Hey, I copied this over and it worked great, but when I copied it over into my own app it didn't display any of the text so I had a blank drop down. Any idea why?
    It was the second page in my app - could the button leading to that page have done anything?

    ReplyDelete
  5. i need to select item in dropdown list box and submit button is clicked it will move to nxt page in android code

    ReplyDelete
  6. i want set in spppiner list only one string how?

    ReplyDelete
  7. How can I populate spinner from database?

    ReplyDelete
  8. Hii, i want to show drop down list on full screen, how i can do it?

    ReplyDelete
  9. How can i show drop down list on full screen

    ReplyDelete

Popular Posts