Android: Cannot style spinner divider

Russ picture Russ · Jun 6, 2012 · Viewed 27k times · Source

I'm trying to create a theme for my first Android app, and it is driving me round the bend. I finally managed to figure out how to style items in a dropdown list, but now I can't change the colour of the divider between list items. I have searched similar questions on stackoverflow, and tried dozens of combinations, but nothing seems to work.

Here is my styles.xml file (abbreviated for clarity):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="android:Theme.Light">
    <item name="android:spinnerStyle">@style/spinnerStyle</item>
    <item name="android:spinnerDropDownItemStyle">@style/spinnerDropDownItemStyle</item>    
    <item name="android:dropDownListViewStyle">@style/spinnerListViewStyle</item>
  </style>

  <style name="spinnerStyle" parent="@android:style/Widget.Spinner">
      <item name="android:background">@drawable/my_theme_spinner</item>
  </style>

  <style name="spinnerDropDownItemStyle" parent="@android:style/Widget.DropDownItem.Spinner">
      <item name="android:background">@drawable/my_theme_spinner_item</item>
      <item name="android:paddingLeft">5dp</item>
      <item name="android:gravity">center_vertical</item>
  </style>

  <style name="spinnerListViewStyle" parent="@android:style/Widget.ListView.DropDown">
      <item name="android:height">3dp</item>
      <item name="android:dividerHeight">3dp</item>
      <item name="android:divider">@color/divider</item>
  </style>
</resources>

No matter what I do, I just get a 1dp light grey divider between items (which can barely be seen with my light coloured list item background) - neither the height nor colour of the divider is ever affected (I also tried setting it to a drawable, also with no effect). What am I doing wrong?

Answer

christophercotton picture christophercotton · Oct 30, 2012

I have a very simple Activity with the Spinner and it works for the following. The only difference I see is that you have a <item name="android:height">3dp</item> and I don't have that at all.

<style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
    <item name="android:divider">#ff0000</item>
    <item name="android:dividerHeight">5dp</item>
</style>


<style name="SampleTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:dropDownListViewStyle">@style/TestSpinnerStyle</item>
</style>

and in my Activity I have:

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    List<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(dataAdapter);

and then for the main layout I have the following XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, StylingActivity"
            />
    <Spinner android:id="@+id/spinner"
             android:layout_width="250dp"
             android:layout_height="40dp"
             />
</LinearLayout>

Here is the screenshot

sample activity screnshot

If you can't get it to work from there, I can push up the entire thing to a github repo for you.