Spinner ComboBox DropDown List Android example code

Spinner in Android application is equivalent of ComboBox in WinApi.
Spinner get selected item to string example.
Spinner get selected item position to int example.

Main activity class MainComboBox.java

public class MainComboBox extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.countries_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
 
        Button myButton =(Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
 
            Spinner sp =	(Spinner)findViewById(R.id.spinner);
            String spinnerString = null;
            spinnerString = sp.getSelectedItem().toString();
            int nPos = sp.getSelectedItemPosition();
 
 
                Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString,
                    Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos,
                		Toast.LENGTH_LONG).show();
            }
        });
    }
}

File MyOnItemSelectedListener.java

public  class MyOnItemSelectedListener implements OnItemSelectedListener {
 
    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
      Toast.makeText(parent.getContext(), "Item is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }
 
    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

strings.xml

<resources>
    <string name="hello">Hello World, MainComboBox!</string>
    <string name="app_name">ComboBox</string>
    <string name="prompt">Choose a country</string>
    <string-array name="countries_array">
        <item>China</item>
        <item>India</item>
        <item>USA</item>
        <item>Indonesia</item>
        <item>Brazil</item>
        <item>Pakistan</item>
        <item>Nigeria</item>
        <item>Bangladesh</item>
        <item>Russia</item>
    </string-array>    
</resources>

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/prompt"
    />
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/prompt"
    />
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 
</LinearLayout>
This entry was posted in Android Examples Code and tagged , , , , . Bookmark the permalink.


Comments are closed.

26,438 views