Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Drawing a button as a spinner

Tags:

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?

like image 713
Sir_Lagsalot Avatar asked Dec 23 '11 18:12

Sir_Lagsalot


2 Answers

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

like image 188
Jonathan Lin Avatar answered Sep 22 '22 11:09

Jonathan Lin


I found a solution to my question. In the constructor for my button I set the background drawable resource to be "btn_dropdown".

public DateAndTimePicker(Context context) {     super(context);     this.setBackgroundResource(android.R.drawable.btn_dropdown); } 

It looks the same as the spinner buttons, none of the button behavior has changed, and the button text centers correctly.

like image 44
Sir_Lagsalot Avatar answered Sep 21 '22 11:09

Sir_Lagsalot