It works in a similar way to that of the the listView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Spinner"
/>
</LinearLayout>
when you click on the spinner it popups like this:
To handle the selected item you can use do it like this:
final String [] items=new String[]{"Item1","Item2","Item3","Item4"};The above code displays the selected item text in the textview.
ArrayAdapterad=new ArrayAdapter (this,android.R.layout.simple_spinner_item,items);
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin=(Spinner)findViewById(R.id.Spinner);
spin.setAdapter(ad);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
TextView txt=(TextView)findViewById(R.id.txt);
TextView temp=(TextView)arg1;
txt.setText(temp.getText());
}
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
});
The parameters of the OnItemClick method are:
• Arg0:the Spinner, notice that it is of type AdapterView.
• Arg1: the view that represents the selected item, in this example it will be a TextView
• Arg2: the position of the selected item.
• Arg3: the id of the selected item.
and that's it for the Spinner Control.
No comments:
Post a Comment