86

I'm trying to apply a linear gradient to my ListView. This is the content of my drawable xml:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#3A3C39" 
        android:endColor="#181818"
        android:angle="270"
     />
    <corners android:radius="0dp" />
</shape>

So I apply it to my ListView with:

android:background="@drawable/shape_background_grey"

It works but it looks very "banded" on emulator and on a real device too.

Is there any way to reduce this "behaviour"?

kiamlaluno
  • 24,790
  • 16
  • 70
  • 85
Francesco Laurita
  • 22,784
  • 7
  • 52
  • 63
  • 18
    Just an update: adding getWindow().setFormat(PixelFormat.RGBA_8888); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER); in the onCreate method seems does the trick also for hdpi with amoled screen (N1/Desire) – Francesco Laurita Jun 08 '10 at 13:40
  • 2
    `@Francesco` Great! It helped on my **Galaxy S** with **Android 2.2**. Please convert your useful comment to an answer, so people can vote for it. – java.is.for.desktop Jun 10 '11 at 22:49

7 Answers7

85

As Romain Guy suggests:

listView.getBackground().setDither(true);

solves my problem

If this is not enough especially for AMOLED and/or hdpi devices try this:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}
Francesco Laurita
  • 22,784
  • 7
  • 52
  • 63
40

You can simply enable dithering on your Drawable object.

Romain Guy
  • 95,351
  • 17
  • 214
  • 199
  • 1
    Yes it works! Ty very much. BTW I tried to use this property into the drawable xml definition but as I read better into the documentation, this property is supported only for the selector element. – Francesco Laurita May 28 '10 at 19:47
  • Unfortunly this not works on Android version > 1.5. I tested on real device with Android 1.6 and Android 1.5. Adding dither to my drawable gradient not works. The devices are both 320 x 480 (180 ppi). Any suggestion? Tnx – Francesco Laurita May 31 '10 at 09:52
  • 5
    `setDither()` was deprecated in API level 23. This property is now ignored. – Tot Zam Mar 17 '17 at 00:58
9

Put this in your Activity:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}
Miša Peić
  • 6,373
  • 1
  • 17
  • 15
3

If the dither and RGBA_888 setting don't help on some phones, the solution can be to set the element layerType to software, to disable the hardware acceleration

android:layerType="software"

This fixed my problem on a Samsung S5 phone.

peter.bartos
  • 10,745
  • 2
  • 46
  • 60
3

For me on HTC Desire works like this

window.getDecorView().getBackground().setDither(true);
Paresh Mayani
  • 122,920
  • 69
  • 234
  • 290
Sebastian
  • 206
  • 3
  • 6
2

For me the banding disappeared when I set the android:useLevel="true" on the gradient: gradient_dark.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#464646"
        android:endColor="#323232"
        android:angle="270"
        android:type="linear"
        android:useLevel="true"
         />
</shape>

But first I tried using a Layer-List with a "noise" drawable to remove the banding, it helps but there is still some banding.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/gradient_dark"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/noise_repeater"
            android:tileMode="repeat" />
    </item>

</layer-list>

The "noise_repeater" drawable i created and used

enter image description here

TouchBoarder
  • 6,285
  • 2
  • 49
  • 60
0

The only thing that worked for me was to set a slightly-transparent alpha channel on both the startColor and endColor.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#FE3A3C39" 
        android:endColor="#FE181818"
        android:angle="270"
     />
</shape>

I got the idea to try this from this other SO question: android:dither=“true” does not dither, what's wrong?

Community
  • 1
  • 1
Greg
  • 41
  • 7