431

I have created a specific List which exists out of the following elements to create a scrollable list with every row containing a Image on the left side and some text on the right side:

To begin with a "root" layout :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="#C8C8C8"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:divider="#C8C8C8"
        android:background="#C8C8C8"/>
</LinearLayout>

and then within the ListView I place the following "row" item :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/bg_row"
>
    <ImageView
        android:layout_width="wrap_content"
        android:paddingLeft="10px"
        android:paddingRight="15px"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_image"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:textSize="16sp"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:maxHeight="50px"/>
</LinearLayout>

As long as the screen is shown statically (as in no movement) it will be shown correctly, but when I start scrolling through the list the background of the row-item (an "icon" as can be shown in the code) will be shown correctly but the background of the "root" layout will become completely black... when the scrolling stops the background will, most of the times, get back its color... As I test I also added a TextView in that root-element with the same background, this one will detain it's color when the List is scrolled... Any idea why this is happening, and how to solve this?

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
TiGer
  • 5,879
  • 6
  • 35
  • 36

12 Answers12

771

Add an attribute on the ListView Tag

android:cacheColorHint="#00000000" // setting transparent color

For more details check this blog

Piyush
  • 18,895
  • 5
  • 32
  • 63
Praveen
  • 90,477
  • 74
  • 177
  • 219
  • 23
    this.getListView().setCacheColorHint(0); From layout I couldn't set the color. But I managed to do it with the above code. I have used this code in 'onCreate' of the list activity. Hope this information also helps. – Dijo David Apr 13 '11 at 08:02
  • 70
    `android:cacheColorHint="#0000"` also works if you're afraid of zeros. – Andrew T. Dec 01 '11 at 22:04
  • 137
    Or also `android:cacheColorHint="@android:color/transparent"` if you're afraid of numbers :) – dule Feb 10 '12 at 21:44
  • 2
    @android:color/transparent probably has the added bonus of having one single bit set. There is a bug where if you pass 0 on some phones transparency wont work. So the trick is to set 1 bit. – Thomas Dignan Nov 10 '12 at 00:01
62

It's very simple just use this line in your layout file :

android:scrollingCache="false"

like this:

<ListView 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollingCache="false"
/>
Mahesh
  • 2,862
  • 2
  • 31
  • 41
  • 2
    It works but I don't think it is smart to do: http://stackoverflow.com/questions/15570041/scrollingcache – Morten Holmgaard Feb 15 '14 at 15:24
  • 1
    I had a case in which android:cacheColorHint="#00000000" did not work, but android:scrollingCache="false" did. Thanks. – Yar Feb 02 '15 at 14:08
27

you can use like this:

list.setCacheColorHint(Color.TRANSPARENT);
list.requestFocus(0);
nikki
  • 3,248
  • 3
  • 24
  • 29
10

android:id="@android:id/list"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:divider="#C8C8C8"
android:background="#C8C8C8"
android:cacheColorHint="#00000000"/>
AnDx
  • 824
  • 8
  • 11
8

We have plenty of options for this problem, you can set the background as transparent through programming like

yourlistview.setCacheColorHint(Color.TRANSPARENT); 

or through xml

android:cacheColorHint="@android:color/transparent"
Sampath Kumar
  • 4,433
  • 2
  • 27
  • 42
5

In your xml where to use Listview set

    android:cacheColorHint="@android:color/transparent"
Charuක
  • 12,953
  • 5
  • 50
  • 88
jeet parmar
  • 868
  • 8
  • 19
5

I am using images in listView and it turns black sometimes in samsung s4 not even scrolling. It was a stupid mistake which I have done in the adapter.I just put my view to null to fix this issue

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            convertView = null; // convert view should be null
            if (convertView == null) {
                holder = new Holder();
                convertView = inflater1.inflate(R.layout.listview_draftreport_item, null);
             } 
        }
Charuක
  • 12,953
  • 5
  • 50
  • 88
Ahmad Arslan
  • 4,498
  • 8
  • 38
  • 59
4

There are allot of answers to this question but today I realized this question is still missing a critical piece of information.

There are two possible solutions for the problem, both work but each should be used in different situations.


Methods

Use android:cacheColorHint when your ListView has a solid color background.

<item name="android:cacheColorHint">@android:color/transparent</item>

Use android:scrollingCache when your ListView has a (complex) image as background.

<item name="android:scrollingCache">false</item>

Note

When your ListView has a solid color background both methods will work, so not only the cacheColorHint will work. But it's not recommended to use the scrolingCache method for solid color backgrounds since it turns off an optimization method used for smooth animating and scrolling the ListView.

Note for the note: scrolingCache set to false does not necessarily mean the ListView's animations and scrolling will become slow.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
2
android:cacheColorHint="@android:color/transparent"
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
muditagarwal88
  • 548
  • 5
  • 13
2

The following worked for me:

myListView.setScrollingCacheEnabled(false);
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
1

android:cacheColorHint="#00000000"// setting transparent color

or

don't set background of listview.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43
1

Add an attribute

android:cacheColorHint="#00000000" //transparent color
Røhäñ Dås
  • 528
  • 4
  • 13