Android Spinner Example - javatpoint [Link]
com/android-spinner-example
Content Menu ▼
1 de 4 27/12/2015 17:45
Android Spinner Example - javatpoint [Link]
Android Spinner Example
Android Spinner is like the combox box of AWT or Swing. It can be used to
display the multiple options to the user in which only one item can be selected
by the user.
Android spinner is like the drop down menu with multiple values from which
the end user can select only one value.
Android spinner is associated with AdapterView. So you need to use one of the
adapter classes with spinner.
Android Spinner class is the subclass of AsbSpinner class.
Android Spinner Example
In this example, we are going to display the country list. You need to use
ArrayAdapter class to store the country list.
Let's see the simple example of spinner in android.
activity_main.xml
Drag the Spinner from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
<RelativeLayout xmlns:androclass="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" />
</RelativeLayout>
Activity class
Let's write the code to display item on the spinner and perform event handling.
File: [Link]
package [Link];
import [Link];
import [Link];
2 de 4 27/12/2015 17:45
Android Spinner Example - javatpoint [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity implements
[Link] {
String[] country = { "India", "USA", "China", "Japan", "Other", };
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById([Link].spinner1);
[Link](this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,[Link].simple_spinner_item,country);
[Link]([Link].simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
[Link](aa);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
[Link](getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link].activity_main, menu);
return true;
}
}
download this example
3 de 4 27/12/2015 17:45
Android Spinner Example - javatpoint [Link]
Output:
← prev next →
Share 0
4 de 4 27/12/2015 17:45