Scrolling a ListView changes everything from White to Black

Android

Android Problem Overview


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!

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

ListView lv = getListView();
setCacheColorHint(0);

Set the cache color hint to zero.

Solution 3 - Android

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

android:cacheColorHint="#000000"

Solution 4 - Android

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"/>



Solution 5 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCameron McBrideView Question on Stackoverflow
Solution 1 - AndroidRomain GuyView Answer on Stackoverflow
Solution 2 - AndroidSababadoView Answer on Stackoverflow
Solution 3 - AndroidGoogleView Answer on Stackoverflow
Solution 4 - AndroidTripaty SahuView Answer on Stackoverflow
Solution 5 - AndroidLeo AccendView Answer on Stackoverflow