4

I want to be able to turn off overscroll (the glowing effect when reaching the top or bottom of a page in 2.3+) however I also want my code to run in older versions of android that don't even have overscroll functionality. As per the documentation here: Android Backwards Compatibility I am using reflection in my custom webview class to call setOverScrollMode however everytime I call this on a device running 2.3.4, I get a NoSuchMethodException. Any idea why I can't retrieve this method?

Strangely, if I just call setOverScrollMode without any reflection, it works, so the method is definitely there.

public class MyWebView extends WebView{

    public void compatibilitySetOverScroll(){
    try {
        Method mWebview_SetOverScroll = WebView.class.getMethod("setOverScrollMode", new Class[] { Integer.class } );
        /* success, this is a 2.3+ */
        if (mWebview_SetOverScroll != null) {
            try {
                mWebview_SetOverScroll.invoke(this, 2);
            } catch (InvocationTargetException ite) {       
                throw new RuntimeException(ite.getCause());
            } catch (IllegalAccessException ie) {
                System.err.println("unexpected " + ie);
            }               
        }
    } catch (NoSuchMethodException nsme) {
        /* failure, must be older device */
    }        
}
}
1
  • Thanks for this question,it solved my problem too,but can you tell me why we need to use reflection?why it throws java.lang.VerifyError when firstly i direct use "setOverScrollMode(OVER_SCROLL_NEVER);" in if(version>10) clause?
    – topxebec
    Jul 30, 2012 at 9:07

3 Answers 3

1

Try Integer.TYPE instead of Integer.class

1
  • 1
    That was it! Sometimes all it takes is a fresh set of eyes. Thanks.
    – Pete
    Jul 14, 2011 at 6:53
1

More correct version:

public static void disableOverscroll(View view) {
    Class<?> viewCls = view.getClass();
    try {
        Method m = viewCls.getMethod("setOverScrollMode",
                new Class[] { int.class });
        int OVER_SCROLL_NEVER = (Integer) viewCls.getField(
                "OVER_SCROLL_NEVER").get(view);
        m.invoke(view, OVER_SCROLL_NEVER);
    } catch (Exception e) {
        // swallow
    }
}
1

another way :

try
{
    Class<?> myTarget = Class.forName("android.widget.HorizontalScrollView");
    Method myMethod = myTarget.getDeclaredMethod("setOverScrollMode", Integer.TYPE);
    myMethod.invoke(scrollView, 2);
}
catch (Exception e)
{
    e.printStackTrace();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.