Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Adding GestureDetector to webview

I would like to add the GestureDetector class to my webview so that I can manage common gestures such as doubletap.

Here is what I am attempting to accomplish:

  • SingleTap will function normally
  • DoubleTap will open items (more on this below)

Regarding the DoubleTap - The site in question has source code that defines how single and double clicks are handled for certain items. It states that a single click selects an item and a double click opens that item. I have to be vague regarding the items since it is not a public site.

I do not need to configure flings or swipes. I am just trying to recreate PC Mouse single clicks and double clicks.

EDIT: I added in the suggestions below from Pat. I changed a few things to my liking and added some from other sources.

I used this question to help build my Activity along with Pat's help. Using the "First Way" code, it loads my app correctly and successfully reloads the page as designed. Now, I just need to add the other event handlers and configure them correctly. Will post final code when I get it to work and I'll mark Pat's answer as correct.

This user is asking the same question with no answer: How to make double clicks work.

It says that this is depreciated GestureDetector(new MyGestureDetector()); - should I fix this?

EDIT 2: Since the actual question asked was how to add gesturedetecor to webview and I was able to do that, I will mark Pat's answer as correct. Even though I did want to make doubleTap = double click, I did not ask that in my title so I will create a new question for that.

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class MainActivity extends Activity{
    WebView webview;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);   
        //Do we want/need to enable Java?
        webview.getSettings().setJavaScriptEnabled(true); 
        //Here we allow for zoom controls - pinch
        webview.getSettings().setBuiltInZoomControls(true);
        //Here we remove the zoom control buttons - requires API 11
        webview.getSettings().setDisplayZoomControls(false);
        //Here we clear the Cache and SSL Preferences       
        webview.clearCache(true);
        webview.clearSslPreferences();
        //Do we need to enable scroll bars to allow people to scroll left and right?        
        webview.setHorizontalScrollBarEnabled(true);
        webview.setVerticalScrollBarEnabled(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("website");

        final GestureDetector gd = new GestureDetector(new MyGestureDetector());
        View.OnTouchListener gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gd.onTouchEvent(event);
            }
        };
        webview.setOnTouchListener(gl);
    }

class MyGestureDetector extends SimpleOnGestureListener {    
    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        Log.i("", "DoubleTapEvent");
        return true;
        }   

    }    


// Ignore SSL certificate errors    
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

//Would like to have a Menu Button to refresh the page - or really just bring you to the login page - for use when the session times out    
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(1, 1, 0, "Refresh");
    //removed below to have only buttons show
    //getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    case R.id.refresh:
        webview.loadUrl("website");
        return true;
    }
    return super.onOptionsItemSelected(item);

}

}

~ Dan

like image 684
Dan Avatar asked Dec 20 '13 19:12

Dan


1 Answers

Are you asking how to set a listener to handle single/double taps? Subclass GestureDetector.SimpleOnGestureListener and set that as a touch listener on your:

public class GestureListener extends GestureDetector.SimpleOnGestureListener {

and then override the methods onSingleTapConfirmed for a single tap and onDoubleTap. Then set a touch listener on your webview and onTouch just call the onTouchEvent of your GestureListener.

Edit:

 final GestureDetector gestureDetector = new GestureDetector(context, new GestureListener());

                webview.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return gestureDetector.onTouchEvent(event);
                    }
                });
like image 199
pat Avatar answered Sep 20 '22 17:09

pat