Spinner Item selection

by / Tuesday, 06 August 2013 / Published in Android

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

code for layout:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

code for activity:

Spinner spinner;

spinner=(Spinner)findViewById(R.id.spinner);

String item[]={
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, item);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {

Toast.makeText(MainActivity.this, "Month is selected", Toast.LENGTH_SHORT).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});

 

Tagged under: ,
TOP