Android: Using linear gradient as background looks banded

AndroidGradientShape

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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);
}

Solution 2 - Android

You can simply enable dithering on your Drawable object.

Solution 3 - Android

Put this in your Activity:

@Override
public void onAttachedToWindow() {
	super.onAttachedToWindow();
	Window window = getWindow();
	window.setFormat(PixelFormat.RGBA_8888);
}

Solution 4 - Android

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.

Solution 5 - Android

For me on HTC Desire works like this

window.getDecorView().getBackground().setDither(true);

Solution 6 - Android

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

Solution 7 - Android

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?

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
QuestionFrancesco LauritaView Question on Stackoverflow
Solution 1 - AndroidFrancesco LauritaView Answer on Stackoverflow
Solution 2 - AndroidRomain GuyView Answer on Stackoverflow
Solution 3 - AndroidMiša PeićView Answer on Stackoverflow
Solution 4 - Androidpeter.bartosView Answer on Stackoverflow
Solution 5 - AndroidSebastianView Answer on Stackoverflow
Solution 6 - AndroidTouchBoarderView Answer on Stackoverflow
Solution 7 - AndroidGregView Answer on Stackoverflow