3

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?

f4t-t0ny
  • 103
  • 6
  • I think you need to also make your own copy of ViewPager class and make it use your EdgeEffect object instead of the systems. – FoamyGuy Feb 18 '13 at 16:34
  • Ok, but if I do that i get a Dex Loader Error: Multiple dex files define Landroid/support/v4/view/ViewPager$Decor; – f4t-t0ny Feb 18 '13 at 16:39
  • you need to put yours in your own package. And inside of your xml refer to it like `` – FoamyGuy Feb 18 '13 at 16:41
  • Thanks a lot for your help! Yust messing with some build errors, but i 'll give some notice, if it's working :-) – f4t-t0ny Feb 18 '13 at 17:02
  • Hi! Had to implement ViewPager, PagerAdapter, FragmentstatePagerAdapter, EdgeEffectCompat, EdgeEffectCompatIcs and EdgeEffect, but it worked. Again, Thank's a lot for your help! – f4t-t0ny Feb 18 '13 at 18:44
  • Dang, lot of work to get a different edge effect =x. Glad you got it working though. If you have time post your solution as an Answer to this question so others can benefit from it in the future. – FoamyGuy Feb 18 '13 at 19:39

3 Answers3

8

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) {
    }
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
2

You have to implement ViewPager, PagerAdapter, FragmentstatePagerAdapter, EdgeEffectCompat, EdgeEffectCompatIcs and EdgeEffect in a package of your own app (for example com.yourapp.viewpager). Only changes made was adjusting the imports and packages names. Copy and edit resources files to res/drawable of your app and et voila, it work's.

Macarse
  • 91,829
  • 44
  • 175
  • 230
f4t-t0ny
  • 103
  • 6
1

I can suggest a very simple way, but hackish:

int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY);

I took advantage of the fact that the glow effect is actually a shared Drawable and applied a filter on it: http://evendanan.net/android/branding/2013/12/09/branding-edge-effect/

Menny
  • 473
  • 6
  • 13
  • This seems to be working nicely for me so far. The accepted answer calls for a lot of boring work, the most voted one for reflection, so this one, although hackish, is the least bad one. – Nemanja Kovacevic Aug 26 '16 at 14:49