Stack Overflow archive
3 score

ScrollView : Change the edge effect color with Holo

score
3
question views
10.8K
license
CC BY-SA 3.0

You can change the EdgeEffect color of a ScrollView with some reflection:

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

@SuppressLint("NewApi")
public static void setEdgeEffectColor(final EdgeEffect edgeEffect, final int color) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            edgeEffect.setColor(color);
            return;
        }
        final Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
        final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
        edgeField.setAccessible(true);
        glowField.setAccessible(true);
        final Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
        final 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 (final Exception ignored) {
    }
}

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