android – ListView top highlight on scrolling-ThrowExceptions

Exception or error:

The border displays a default color (that’s orange on my Nexus S) while scrolling a ListView to the limit. How to change that color?

I really don’t know how to explain it. Just look at this picture:

android list view top highlight on scrolling

So, how to change the highlight color when the ListView scrolling to the border? using themes or styles

How to solve:

The solution is to use setOverscrollFooter(null) and setOverscrollHeader(null).
The documentation is here !

You can also set it directly in the XML :

<ListView android:overScrollMode="never" />

Or specify the footer and the header :

<ListView 
  android:overscrollHeader="@null" 
  android:overscrollFooter="@null" />

N.B. : There is also a property fadingEdge that may interest you.

“Overscroll” methodes are supported starting API level 9

###

Finally I found the solution.

  1. setOverscrollFooter(null) and setOverscrollHeader(null) does not work. At least on 2.3.*. Setting attributes from *.xml doesn’t help too.
  2. setOverScrollMode(View.OVER_SCROLL_NEVER) causes glitchy scrolling. At least on 2.3.*.

The only solution that really works involves the use of Java Reflection.
It works even with ugly custom Samsung listviews with bounce overscroll effect.
Here is a snippet:

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    //onOverScrolled method must be overrided, or we will see the background of the listview when overscroll fast.
}

private void removeOverscrollEffect() {
    try {
        Class<?> superClass = getClass().getSuperclass().getSuperclass();

        Field field = superClass.getDeclaredField("mEdgeGlowTop");
        field.setAccessible(true);
        Object edgeGlowTop = field.get(this);
        if (edgeGlowTop != null) {
            Class<? extends Object> edgeClass = edgeGlowTop.getClass();
            Field edgeDrawable = edgeClass.getDeclaredField("mEdge");
            edgeDrawable.setAccessible(true);
            edgeDrawable.set(edgeGlowTop, new ColorDrawable(Color.TRANSPARENT));
            Field glowDrawable = edgeClass.getDeclaredField("mGlow");
            glowDrawable.setAccessible(true);
            glowDrawable.set(edgeGlowTop, new ColorDrawable(Color.TRANSPARENT));
            field.set(this, edgeGlowTop);
        }

        Field fieldBottom = superClass.getDeclaredField("mEdgeGlowBottom");
        fieldBottom.setAccessible(true);
        Object edgeGlowBottom = fieldBottom.get(this);
        if (edgeGlowBottom != null) {
            Class<? extends Object> edgeClassBottom = edgeGlowBottom.getClass();
            Field edgeDrawableBottom = edgeClassBottom.getDeclaredField("mEdge");
            edgeDrawableBottom.setAccessible(true);
            edgeDrawableBottom.set(edgeGlowBottom, new ColorDrawable(Color.TRANSPARENT));
            Field glowDrawableBottom = edgeClassBottom.getDeclaredField("mGlow");
            glowDrawableBottom.setAccessible(true);
            glowDrawableBottom.set(edgeGlowBottom, new ColorDrawable(Color.TRANSPARENT));
            fieldBottom.set(this, edgeGlowBottom);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

I hope this helps.

###

Here is a nice article on ListView Backgrounds Optimization.

To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000"

###

Use it in XML file–

<ListView ---
     android:fadingEdge="none"
   ---</ListView>

EDITED:

Using fading edges may introduce noticeable performance degradations and should be used only when required by the application’s visual design. To request fading edges with API level 14 and above, use the android:requiresFadingEdge attribute instead.

Check this API link

###

I used kord’s answer until it stopped working in Lollipop, so I changed into this:

try {
    Class<?> superClass = getClass().getSuperclass().getSuperclass();

    Field field = superClass.getDeclaredField("mEdgeGlowTop");
    field.setAccessible(true);
    field.set(this, new NoEdgeEffect(getContext()));

    Field fieldBottom = superClass.getDeclaredField("mEdgeGlowBottom");
    fieldBottom.setAccessible(true);
    fieldBottom.set(this, new NoEdgeEffect(getContext()));
} catch (Exception e) {
}

class NoEdgeEffect extends EdgeEffect
{
    public NoEdgeEffect(Context context) {
        super(context);
    }
    public boolean draw(Canvas canvas) {
        // Do nothing
        return false;
    }
}

###

you can use android:listSelector=”#002234″.

In above value can be any color code that you can find on internet easily.

Leave a Reply

Your email address will not be published. Required fields are marked *