53

I have a custom list view and I want the background of the list to be white, so I do something like this which works great.

listView = (ListView) this.findViewById(R.id.listview);
listView.setBackgroundColor(Color.WHITE);

The problem is when you scroll the list the background of all the list items changes to black, which looks horrible.

I tried in my list view setting the background color to white. When I inflate the view I also tried setting the background color to white:

view.setBackgroundColor(Color.WHITE);

Both of these fix the problem of the scrolling background color, but now the item doesn't appear to be clickable even though it is. What I mean by that is the onClick still works fine, but the background doesn't flash to orange to let the user know he clicked it.

How can I have a white background in a list view, that stays white while scrolling, and does the normal list acitvity orange click background?

Thanks!

Cameron McBride
  • 6,779
  • 11
  • 40
  • 52

5 Answers5

118

The solution is very simple, you also need to set the cache color hint to white: setCacheColorHint(Color.WHITE). You don't need to change the list items' background color.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 1
    That worked perfectly. listView = (ListView) this.findViewById(R.id.listview); listView.setBackgroundColor(Color.WHITE); listView.setCacheColorHint(Color.WHITE); – Cameron McBride Jun 10 '10 at 14:04
  • 36
    listView.setCacheColorHint(Color.TRANSPARENT); or android:cacheColorHint="#00000000" //Reuse background color – CelinHC Oct 20 '11 at 15:41
  • transparent didn't work, white did – max4ever Aug 29 '12 at 13:31
  • @max4ever, can you tell your android version? 2.3.3. transparent works well. – neworld Sep 19 '12 at 09:59
  • For more details on this issue take a look at this android dev blog post: http://android-developers.blogspot.co.nz/2009/01/why-is-my-list-black-android.html – TheIT Feb 06 '14 at 22:19
  • in XML define listview __android:cacheColorHint="@android:color/transparent"__ – vuhung3990 Nov 11 '14 at 08:21
7
ListView lv = getListView();
setCacheColorHint(0);

Set the cache color hint to zero.

Sababado
  • 2,524
  • 5
  • 33
  • 51
6

Solution is very simple, you also need to set the cache color hint to black in xml.

android:cacheColorHint="#000000"
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
Google
  • 2,183
  • 3
  • 27
  • 48
3

Android attempts to improve the performance of ListView scrolling by caching layout information. If you have long scrolling lists of data you should also set the the android:cacheColorHint property on the ListView declaration in the Activity’s AXML definition (to the same color value as your custom row layout’s background). Failure to include this hint could result in a ‘flicker’ as the user scrolls through a list with custom row background colors. So give the same color to the listitem's background and android:cacheColorHint.you can refer below code.

In my Adapter layout I gave as

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ADAPTER_CONTAINER_PRIMARY"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FAFAEE"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center">
     .....

And in the List view

<ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/LIST_GRID_LIST_INSTANCES"
            android:focusableInTouchMode="true"     
            android:smoothScrollbar="true"
            android:divider="@drawable/Gray"
            android:dividerHeight="0.05dp"
            android:cacheColorHint="#FAFAEE"
            android:background="@drawable/round"/>
Tripaty Sahu
  • 955
  • 4
  • 13
  • 32
1

If your ListView is drawn atop a more complex background -- say, a bitmap or gradient -- then you may need to disable the scroll cache entirely. I ended up setting android:scrollingCache="false" on my ListView to resolve this issue.

http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:scrollingCache

Leo Accend
  • 321
  • 2
  • 10