Stack Overflow archive
1 score

Change HorizontalScrollView's light color - OverScrollMode

score
1
question views
2.7K
license
CC BY-SA 3.0

You can set the EdgeEffect color using reflection. The following will work from API 14+:

java
public static void setEdgeGlowColor(final HorizontalScrollView hsv, final int color) {
    try {
        final Class<?> clazz = HorizontalScrollView.class;
        for (final String name : new String[] {
                "mEdgeGlowLeft", "mEdgeGlowRight"
        }) {
            final Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            setEdgeEffectColor((EdgeEffect) field.get(hsv), color);
        }
    } catch (final Exception ignored) {
    }
}

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");
        edgeField.setAccessible(true);
        final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
        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.