Search code examples
androidlistviewgradientdivider

Android ListView : bad gradient divider


I have a ListView :

<ListView
    android:id="@+id/mainList"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="1dip"
    android:layout_marginRight="1dip"
    android:divider="@drawable/list_divider"
    android:dividerHeight="1px"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:cacheColorHint="#00000000"
/>

and a shape in list_divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
   <gradient
      android:startColor="#00000000"
      android:centerColor="#FFFFFFFF"
      android:endColor="#00000000"
      android:angle="0" />
</shape>

but instead of having a divider like this : white - gray - black - gray - white
I get this : white - gray - black - gray - white - gray - black - gray - white ???
In short the divider is splitted into two similar parts and I need only one.


Solution

  • For gradient white-gray-black-gray-white use :

    <gradient
       android:startColor="#ffffff"
       android:centerColor="#000000"
       android:endColor="#ffffff"
       android:angle="0">
    

    Double gradient is probably due to Alpha channel you added to your colors. Center color, for example, starts with #00 (8 bytes) and means full transparent so you have the under layer color.