Android Viewpager EdgeEffect custom Color

android, android-viewpager

Solution

You can set the `EdgeEffect` color of the `ViewPager` with some reflection:

public static void setEdgeGlowColor(ViewPager viewPager, int color) {
    try {
        Class<?> clazz = ViewPager.class;
        for (String name : new String[] {
                "mLeftEdge", "mRightEdge"
        }) {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            Object edge = field.get(viewPager); // android.support.v4.widget.EdgeEffectCompat
            Field fEdgeEffect = edge.getClass().getDeclaredField("mEdgeEffect");
            fEdgeEffect.setAccessible(true);
            setEdgeEffectColor((EdgeEffect) fEdgeEffect.get(edge), 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) {
    }
}

Problem

I am trying to customize the EdgeEffect in the Viewpager of my App. The aim was to replace the blue ics Overscroll EdgeEffect i.e. with a custom red one. So at first i edited overscroll_edge and the corresponding overscroll_glow. Then I put them both into the /res/drawable directory of my app. Additionally i copied the EdgeEffect Source File to the /src/android/widget/ directory of my app. The only change i made in EdgeEffect was to import com.my.application.R instead of com.android.internal.R. But Android just won't use my custom android.widget.EdgeEffect instead of the one in Android System, so the Viewpager EdgeEffect stays constantly blue. Am I missing something?

Original source