How to change android spinner popupBackground

I am trying to change the android spinner popup background by setting android: popupBackground, but this had no effect. Is there any way to 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"> 
+7
source share
2 answers

I assume that you are trying to change the "external" backgroud of the Spinner popup, not the Spinner background of the "puppet elements." In addition, I assume that by the popup you mean the Spinner dialog box, where a floating dialog box appears over your action, rather than a new dropdown mode.

Strategy

For a clean, sustainable approach that behaves well across multiple Android platforms and reduces the need for redundancy in the app, I find it important to understand that white papers don't tell us. So follow me on a short trip.

The hard part of Spinners is that the adapter connects to the data. Although itโ€™s relatively easy to define hooks to change the look of android.R.layout.simple_spinner_item and his friends, they define the style of only the Spinner element that is displayed, as well as each individual pop-up element. But Adapter is responsible for creating ListView and other widgets that form this ListView.

This complexity is probably the reason that Android has introduced some attributes that can be specified on the fly for Spinner, although they are then applied to Spnner children, such as android: popupBackground. This is not necessarily a clean approach, but a limited set of convenience features. As for popupBackground, by the way, this was introduced in the level 1 API, but Spinners only respect it in spinnerMode = dropdown, which was introduced in the API level 11. This is the reason you will never be notified if you use it incorrectly.

Older versions of Android (e.g. 2.2)

Listview

Knowing that the Adapter creates a ListView, it would be nice to change the appearance of the ListView in one theme, so there will be one place for changing the design and simple styling:

 <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 Widget.ListView to understand what you can change here] </style> 

Alertlerialog

Unfortunately, there is still more work ahead. Since android: prompt can be used to create a title for a popup, a popup menu really doesnโ€™t just consist of a ListView.

Android uses AlertDialog

Recent versions of Android (e.g. 4.2)

Now that the AlertDialogs are styled, we still have to consider the fact that later versions of Android no longer use the AlertDialogs dialog boxes for Spinner. This will not hurt, because for those, the AlertDialog style must be preserved anyway. It just means that we also need to create a new popup.

To do this, create version-specific XML files to pull extra styles into your custom theme and provide version-specific style XML files.

Feel like trying yourself starting here?

+15
source

android:popupBackground only works when using android:spinnerMode = "dropdown" , which is probably why this did not affect your code. You have to say that the spinner in which it works with some XML.

 android:spinnerMode = "dropdown" 

References

+9
source

All Articles