Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

Detecting Swipe of WebView null Gesture Exception

I am working on adding swipe motion at webview but when it comes to the execution, it comes with exception that the Gesture Detection variable is null. Would you please tell me what els I have to initialise the gesture detector ? The below is my code

Applying part

wv = (CustomWebView)this.findViewById(R.id.webViewX);
    //wv = new CustomWebView(this);
    WebSettings settings = wv.getSettings();
    settings.setDefaultTextEncodingName("UTF-8");
    //settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    //settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    settings.setBuiltInZoomControls(true);

Custom Webview:

public final class CustomWebView extends WebView {

    private GestureDetector gestureDetector;
    private WebView webview;
    private float currentScale;
    private Context ctx;
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomWebView(Context context) {
        super(context);
        this.webview = this;
        ctx = context;
        gestureDetector = new GestureDetector(ctx , new CustomeGestureDetector());
        this.setGestureDetector(gestureDetector);   

    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWebViewClient(new WebViewClient() {
            @Override
            public void onScaleChanged(WebView view, float oldScale, float newScale) {
                super.onScaleChanged(view, oldScale, newScale);
                currentScale = newScale;
            }
        });
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /* 
     * @see android.webkit.WebView#onScrollChanged(int, int, int, int)
     */
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }

    /* 
     * @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
    }

    public void setGestureDetector(GestureDetector gestureDetector) {
        this.gestureDetector = gestureDetector;
    }

    private class CustomeGestureDetector extends SimpleOnGestureListener {      
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if(e1 == null || e2 == null) return false;
            if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
            else {
                try { // right to left swipe .. go to next page
                    if(e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 800) {
                        //do your stuff
                        String url = "http://www.stackoverflow.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url)); 
                        ctx.startActivity(i); 
                        return true;
                    } //left to right swipe .. go to prev page
                    else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) {
                        //do your stuff
                        String url = "http://www.google.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url)); 
                        ctx.startActivity(i); 
                        return true;
                    } //bottom to top, go to next document
                    else if(e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 800 
                            && webview.getScrollY() >= currentScale * (webview.getContentHeight() - webview.getHeight())) {
                        //do your stuff
                        return true;
                    } //top to bottom, go to prev document
                    else if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 800 ) {
                        //do your stuff
                        return true;
                    } 
                } catch (Exception e) { // nothing
                }
                return false;
            }
        }
    }

}

Upvotes: 0

Views: 1727

Answers (1)

Shivam Verma
Shivam Verma

Reputation: 8023

I had implemented Gesture Detection on WebViews. Might help you out :

http://vshivam.wordpress.com/2013/09/02/privly-reading-app-integration-and-detecting-swipe-gesture/

Check this out too. This might be a better method of detecting swipe direction IMHO : http://vshivam.wordpress.com/2014/04/28/detecting-up-down-left-right-swipe-on-android/

Upvotes: 1

Related Questions