分享

Make a Double Click work in a WebView

 quasiceo 2015-04-25

I have a Website that has some double click functionality and so with android when you double click in the browser it will just zoom. So I am building a WebView so I can over ride the double click and make it react like it should.

public class myWebView extends Activity{

GestureDetector gs = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView engine = (WebView) findViewById(R.id.web_engine);
    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    engine.getSettings().setSupportMultipleWindows(true);
    engine.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                    return super.onJsAlert(view, url, message, result);
           }
    });
    engine.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

    engine.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gs == null) {
                gs = new GestureDetector(
                        new GestureDetector.SimpleOnGestureListener() {
                            //overrides the double click
                            @Override
                            public boolean onDoubleTapEvent(MotionEvent e) {
                                // SOME CODE THAT SIMULATES A DOUBLE CLICK
                                return true;
                            }
                        });
            }
            gs.onTouchEvent(event);
            return false;
        }
    }); 
    engine.loadUrl("http://www.google.com");
}
}

So the function onDoubleTapEvent(MotionEvent e) will do whatever is in between in the event of a double click. The above code is mostly thanks too Cehm.

So at this point I need to trick it to executing a a double click. Can I just simulate a Couple of click in there?

asked May 2 '12 at 17:28

    
Its a really bad idea to ignore the UI conventions for a platform. The double tap gesture to zoom in is well known and used by users, if you start using it to do different things you're only going to make users angry. –  slayton May 2 '12 at 22:50
    
Well I am doing it only for a specific web site that a large companies uses for its employees and needs the double click functionality. I am not trying to replace the web browser for the android. I am simply trying to make a small app for a very specific purpose. –  user1366422 May 3 '12 at 18:22

1 Answer

Try it:

private GestureDetector gs = null;

private OnTouchListener onTouch = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (gs == null) {
            gs = new GestureDetector(self,
                    new GestureDetector.SimpleOnGestureListener() {
                        @Override
                        public boolean onDoubleTapEvent(MotionEvent e) {

                            //Double Tap
                            snapWebView.zoomIn();//Zoom in
                            return true;
                        }

                        @Override
                        public boolean onSingleTapConfirmed(MotionEvent e) {

                            //Single Tab
                            snapWebView.zoomOut();// Zoom out
                            return false;
                        };
                    });
        }

        gs.onTouchEvent(event);

        return false;
    }
};

Finally, set OnTouch event to WebView

webView.setOnTouchListener(onTouch);

See detail: How to make Double Click in WebView?

answered Mar 31 '14 at 7:58
yuchi_1k91
11417

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多