Sie sind auf Seite 1von 6

Android Spinner

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

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/
android"
xmlns:tools="http://schemas.android.com/tools"
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.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.

File: MainActivity.java
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener
on it

22.

Spinner spin = (Spinner) findViewById(R.id.spinner1);

23.
24.
25.
26.

spin.setOnItemSelectedListener(this);

//Creating the ArrayAdapter instance having the country list


ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spi
nner_item,country);
27.
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdo
wn_item);
28.
//Setting the ArrayAdapter data on the Spinner
29.
spin.setAdapter(aa);
30.
}
31.
32.
33.
//Performing action onItemSelected and onNothing selected
34.
@Override
35.
public void onItemSelected(AdapterView<?> arg0, View arg1, int positio
n,long id) {
36.
Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGT
H_LONG).show();
37.
}
38.
39.
@Override
40.
public void onNothingSelected(AdapterView<?> arg0) {
41.
// TODO Auto-generated method stub
42.
43.
}
44.
45.
@Override
46.
public boolean onCreateOptionsMenu(Menu menu) {
47.
// Inflate the menu; this adds items to the action bar if it is present.
48.
getMenuInflater().inflate(R.menu.activity_main, menu);
49.
return true;
50.
}
51.
}
download this example

Output:

Populate the Spinner with User Choices from String Resource file

if the available choices for your spinner are pre-determined, you can provide them
with a string array defined in a string resource file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>

With an array such as this one, you can use the following code in
your Activity or Fragment to supply the spinner with the array using an instance
of ArrayAdapter:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner
layout
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_drop
down_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

The createFromResource() method allows you to create an ArrayAdapter from


the string array. The third argument for this method is a layout resource that
defines how the selected choice appears in the spinner control.
The simple_spinner_item layout is provided by the platform and is the default
layout you should use unless you'd like to define your own layout for the spinner's
appearance.
call setDropDownViewResource(int) to specify the layout the adapter should
use to display the list of spinner choices (simple_spinner_dropdown_item is another
standard layout defined by the platform).
Call setAdapter() to apply the adapter to your Spinner.

Das könnte Ihnen auch gefallen