Android Spinner
In Android, you can use “android.widget.Spinner” class to render a dropdown box selection list.
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.
You can add a spinner to your layout with the
In Android, you can use “android.widget.Spinner” class to render a dropdown box selection list.
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.
You can add a spinner to your layout with the
Spinner object. You should usually do so in your XML layout with a <Spinner> element.
<Spinner
   
android:id="@+id/spinner"
   
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Here i implements OnItemSelectedListener geting the Spinner selected item. I also set Spinner selected item to a text view (code: selection.setText(items[position]);
1.Spinner
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="fill_parent"
   
android:layout_height="fill_parent"
   
android:orientation="vertical"
>
   
<TextView
       
android:id="@+id/selection"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
/>
   
<Spinner
       
android:id="@+id/spinner"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:drawSelectorOnTop="true"
/>
</LinearLayout>
2.Code
package
com.learnsimply.spinnerexample;
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{
 TextView
selection;
 
Spinner spin;
 
String[] items
= { "India",
"USA",
"England",
"australia",
"japan",
 
  "china",
"korea"
};
 
/** Called when the activity is first
created. */
 
@Override
 
public
void
onCreate(Bundle savedInstanceState) {
 
 super.onCreate(savedInstanceState);
 
 setContentView(R.layout.activity_main);
 
 selection
= (TextView) findViewById(R.id.selection);
 
 Spinner spin = (Spinner) findViewById(R.id.spinner);
 
 spin.setOnItemSelectedListener(this);
 
 ArrayAdapter<String> aa = new
ArrayAdapter<String>(this,
 
   android.R.layout.simple_spinner_item,
items);
 
 spin.setAdapter(aa);
 
}
 
@Override
 
public
void
onItemSelected(AdapterView<?> parent, View v, int
position,
 
  long
id) {
 
 // TODO
Auto-generated method stub
 
 selection.setText(items[position]);
 
}
 
@Override
 
public
void
onNothingSelected(AdapterView<?> arg0) {
 
 // TODO
Auto-generated method stub
 
 selection.setText("");
 
}
 }
Run the code

 
No comments:
Post a Comment