Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change android spinner popupBackground

I was trying to change the android spinner popup window background by setting the android:popupBackground, but it didn't have any effect. Is there any way I can change it?

    <Spinner
           android:id="@+id/eventNameSpinner"
           android:layout_width="160dp"
           android:layout_height="30dp"
           android:layout_alignParentLeft="true"
           android:layout_centerVertical="true"
           android:layout_marginLeft="6dp"
           android:background="@drawable/btn_name"
           android:paddingBottom="2dp"
           android:paddingTop="2dp"
           android:popupBackground="@drawable/bkg">
like image 726
Wenbo Avatar asked Jan 19 '12 08:01

Wenbo


People also ask

What are the spinners in Android user interface?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


2 Answers

I presume you are trying to change the "outer" backgroud of a Spinner's popup, not the background of Spinner "pupup items". Also, I presume that by popup, you mean a Spinner's dialog mode, where a floating dialog window appears on top of your activity, as opposed to the new dropdown mode.

Strategy

For a clean, sustainable approach which behaves well across multiple Android platforms and reduces the need for redundancy in the App, I believe it is essential to understand what the official docs don't tell us. So follow me on a short journey.

The tricky part about Spinners is that an Adapter is used to connect them to data. While it is relatively easy to identify the hooks for changing the appearance of android.R.layout.simple_spinner_item and its friends, those only determine the style of the Spinner's currently displayed item as well as each single popup item. But it is the Adapter which is responsible for creating the ListView and other widgets which frame that ListView.

This complexity is probably the reason why Android has introduced some attributes which can be specified on-the-fly "for" the Spinner although they are then applied to the Spnner's children, such as android:popupBackground. This is not necessarily a clean approach, rather a limited set of convenience functions. Regarding popupBackground, btw, this was introduced in API level 1, but Spinners respect it only in spinnerMode=dropdown, which was introduced in API level 11. That's the reason why you'll never be notified if you use it wrongly.

Older Android Versions (such as 2.2)

ListView

Knowing that the Adapter creates a ListView, it's not a bad idea to change the ListView appearance in one's theme, so there's one single place for the design change and the styling straightforward, like so:

<style name="MyTheme" parent="@android:style/[reference to original theme]" >
    <item name="android:listViewStyle">@style/myListView</item>
    [...]
</style>

<style name="myListView" parent="@android:style/Widget.ListView">
    [check Android's Widget.ListView to understand what you can change here]
</style>

AlertDialog

Unfortunately, there's more work ahead. Because android:prompt can be used to create a headline for the popup, the popup really consists of more than just the ListView.

Android uses an AlertDialog

Recent Android Versions (such as 4.2)

Now that the AlertDialogs are styled, we still have to address the fact that more recent versions of Android don't use AlertDialogs for Spinner dialogs any more. That doesn't hurt, because for those, the AlertDialog style shouldd be kept anyways. It just means we need to style the new popup as well.

To do so, create version specific theme XML files to pull the additional styles into your customized theme, and provide version specific style XML files.

Feel like trying it yourself, starting here?

like image 94
class stacker Avatar answered Oct 14 '22 14:10

class stacker


android:popupBackground is only valid when using android:spinnerMode = "dropdown" , thats probably why it wasnt any effect in your code. You need to tell that spinner which mode its in with some XML.

android:spinnerMode = "dropdown"

Links

  • http://developer.android.com/reference/android/widget/Spinner.html#attr_android:popupBackground
  • http://developer.android.com/reference/android/widget/Spinner.html
like image 37
Entalpi Avatar answered Oct 14 '22 13:10

Entalpi