The Android Dropdown: Spinner

I am currently working on the next release of RpgTracker and I thought I might do another tutorial since I haven’t written one in quite a while.

In Android the dropdown selection widget is called Spinner. Dropdowns are used to select one of a fixed set of values. They are quite helpful to improve usability of apps to make sure only the right values can be chosen.

In this short tutorial you will find two examples with different applications for a spinner:

  • a spinner used as part of an activity
  • a spinner in a dialogue

I have taken the code samples from my own applications. If you have any questions regarding the examples please don’t hesitate

First there is an example from my RpgTracker. In this example I use an Enum to represent the selected values in the activity in which the user can edit one of the objects used in the app:

[sourcecode language=”java” wraplines=”false”]
public Enum ValueType {
DefaultType, NotSoDefaultType, AnotherType
}

public class EditValue extends Activity implements OnItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
//other initialization code
createTypeDropDown(currentType);
}

private void createTypeDropdown(ValueType currentType) {
final Spinner spinner = (Spinner) findViewById(R.id.type);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.type_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
valueType.setSelection(currentType.ordinal());
//do not forget the listener, otherwise nothing happens:
spinner.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
this.type = ValueType.values()[pos];
}

@Override
public void onNothingSelected(AdapterView parent) {
this.type = ValueType.Default;
}

}
[/sourcecode]

So this is the javacode you need to map the spinner to the Enums values. Of course you also need to specify the dropdown / spinner in your layout files and define the values that are shown.

Layout:

[sourcecode language=”xml” wraplines=”false”]
<TextView android:text="@string/type_prompt"/>
<Spinner android:id="@+id/type" android:prompt="@string/type_prompt"/>[/sourcecode]

Values:

[sourcecode language=”xml” wraplines=”false”]
<string-array name="type_array">
<item>Default</item>
<item>Currency</item>
<item>Experience</item>
</string-array>[/sourcecode]

Don’t forget to also add these two snippets to any landscape layout files or to the resources for other languages.

So that’s how you can create a dropdown field and map it to an Enum easily.

You can also use a spinner in a dialogue. The second example is from DroidWeight. I am checking the preferences to see which fields are currently tracked by the user and then offer the user a choice of one of these fields:

[sourcecode language=”java” wraplines=”false”]
private static AlertDialog getChoice(Context context) {
final List<String> tracked = UserPreferences.getTrackedTypes(context);
final String[] items = tracked.toArray(new String[0]);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.menu_showtype);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
UserPreferences.setDisplayField(context, tracked.get(item));
dialog.dismiss();
}
});
final AlertDialog alert = builder.create();
return alert;
}
[/sourcecode]

This entry was posted in android. Bookmark the permalink.

Comments are closed.