Android: How to get Holo Spinner Theme in Android 2.x?

This Android tutorial explains,

  • How to get holo theme for Spinner in Android 2.x
  • How to change the Spinner background style
  • How to change the background color of selected spinner list item

Download “Android Spinner Style Example” SpinnerStyleDemo.zip – Downloaded 4000 times – 1 MB

You can use this site http://www.android-holo-colors.com/ to generate drawables and themes which you can copy into your project.

In Android 2.x, Spinner style looks like the one shown below. The default theme for versions less than 11 is @android:style/Theme.

android-2-spinner

In Android 4.x, Spinner style looks like the one shown below.
android-4-spinner

If an app does not explicitly request a theme in its manifest, Android System will determine the default theme based on the app’s targetSdkVersion to maintain the app’s original expectations:

Android SDK VersionDefault Theme
Version < 11@android:style/Theme
Version between 11 and 13@android:style/Theme.Holo
14 and higher@android:style/Theme.DeviceDefault

Android’s resource system selects the themes for your app automatically based on the platform version of the device it’s running on. For example,

  • If the device version is < 11, it applies the theme defined in res/values folder.
  • If the device version is between 11 and 13, it applies the theme defined in res/values-v11 folder.
  • If the device version is 14 or higher, it applies the theme defined in res/values-v14 folder.
To get the Holo theme for Spinner in Android 2.x, we can define theme in res/values folder.

Android Project

Create an Android project and name it as SpinnerStyleDemo.

res/values folder

strings.xml

Open res/values/strings.xml and replace it with following content.

colors.xml

Create a new file colors.xml in res/values/ to define colors and replace it with following content.

styles.xml

Open res/values/styles.xml and replace it with following content.

themes.xml

Create a new file themes.xml in res/values and replace it with following content. This Android themes.xml defines Holo theme for Spinner widget for devices running on Android 2.x.

res/values-v11 folder

styles.xml

Create a new res/values-v11/styles.xml and replace it with following content.

themes.xml

Create a new res/values-v11/themes.xml and replace it with following content. This Android themes.xml defines theme for Spinner widget for devices running on versions between 11 and 13. Here, we define custom style for Spinner, i.e.)

  • to change the default spinner background color
  • to change the spinner list item highlighted color

res/values-v14 folder

styles.xml

Create a new res/values-v14/styles.xml and replace it with following content.

themes.xml

Create a new res/values-v14/themes.xml and replace it with following content. This Android themes.xml defines theme for Spinner widget for devices running on versions 14 or higher. Here, we define custom style for Spinner, i.e.)

  • to change the default spinner background color
  • to change the spinner list item highlighted color

res/drawable folder

All these drawable XML files refers to drawables in hdpi, mdpi, xhdip folders.

activated_background_holo_light.xml

This drawable file is used by themes.xml defined in values-v11. It defines drawable used as a background for activated items.

btn_radio_holo_light.xml

This drawable file changes the spinner drop down item radio button style.

item_background_holo_light.xml

list_selector_background_transition_holo_light.xml

list_selector_holo_light.xml

spinner_background_holo_light.xml

XML layout file

This Android Spinner style example uses a layout file to display a Spinner. Open activity_main.xml file in res/layout and replace it with the following content.

Output

Place all the nine patch assets, images in drawable (drawable-hdpi, drawable-mdpi and drawable-xhdpi) folder and run the project to get the expected output.

References