Android: Drawing a button as a spinner

Sir_Lagsalot picture Sir_Lagsalot · Dec 23, 2011 · Viewed 24.9k times · Source

I have a custom android view that extends 'Button' and acts as a multi-level spinner - when it is pressed it displays a series of spinner-like dialogs that allow a user to refine their selection. In this case, it allows the user to select the day, then hour, then minute (in 5 min intervals) that they want a pickup to occur. When the dialog is not active, a text description of the current selection is shown on the button.

I want to visually indicate that the user can change the selection by pressing the button, and think that the look of a spinner is exactly what I need for it. I want the look to be the same as other spinners on the device, and be able to easily change the text on the button, but I'm not sure how to achieve that without it being kludgy. How should I go about displaying a button as a spinner-like icon that is consistent with other spinners on the device?

Answer

Jonathan Lin picture Jonathan Lin · May 21, 2013

This is how I managed to draw a button that is styled like a Spinner:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    style="?android:attr/spinnerStyle" />

That's it!

Update

Something like the following should also work:

<style name="SpinnerButtonStyle" parent="android:Widget.Holo.Spinner">
    <item name="android:textColor">@color/entry_field</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>

For more styles take a peek into the SDK, e.g. sdk/platforms/android-18/data/res/values/styles.xml