Stack Overflow archive
4 score

Controlling the color of the fading edge in ListViews

score
4
question views
22.3K
license
CC BY-SA 3.0

You can change the EdgeEffect color of a ListView or GridView using reflection. Copy the following static factory methods into your project and use setEdgeGlowColor(yourListView, yourAwesomeColor);:

java
public static void setEdgeGlowColor(AbsListView listView, int color) {
    try {
        Class<?> clazz = AbsListView.class;
        Field fEdgeGlowTop = clazz.getDeclaredField("mEdgeGlowTop");
        Field fEdgeGlowBottom = clazz.getDeclaredField("mEdgeGlowBottom");
        fEdgeGlowTop.setAccessible(true);
        fEdgeGlowBottom.setAccessible(true);
        setEdgeEffectColor((EdgeEffect) fEdgeGlowTop.get(listView), color);
        setEdgeEffectColor((EdgeEffect) fEdgeGlowBottom.get(listView), color);
    } catch (Exception ignored) {
    }
}

public static void setEdgeEffectColor(EdgeEffect edgeEffect, int color) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            edgeEffect.setColor(color);
            return;
        }
        Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
        Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
        edgeField.setAccessible(true);
        glowField.setAccessible(true);
        Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
        Drawable mGlow = (Drawable) glowField.get(edgeEffect);
        mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mEdge.setCallback(null); // free up any references
        mGlow.setCallback(null); // free up any references
    } catch (Exception ignored) {
    }
}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.