1

My app is simply a WebView to display a specific website. Users are not able to simply use the Chrome app because of the inherent programming in Android which alters the functions of tapping.

For simplicity, let's say the website displays a calendar and on that calendar are boxes. The boxes can either be single clicked to be selected or double clicked to be opened to display the event details.

The website has coding that handles what to do if an event is single or double clicked:

<div class="boardpiece clickable" onclick="select(event, this);" ondblclick="open('5657849');"

I'm completely fine with using longpress to simulate the double click. Here's where my confusion begins. How to I tell my app that when the user performs a SingleTap on the item, it needs to activate the "onclick" the website is calling for? I also need to tell the app that when the user performs a longpress on that same item, activate the "ondblclick" event you see there in the website coding?

Here is my activity code:

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("https://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 onDoubleTap(MotionEvent e) {
            Log.i("", "DoubleTap");
            return true;
        }   

        public boolean onSingleTap(MotionEvent e) {
            Log.i("", "SingleTap");
            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, "Reload");
    //getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

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

}

}
Dan
  • 85
  • 1
  • 9
  • Is it something like this? Where I have to call javascript functions? http://stackoverflow.com/questions/4065312/detect-click-on-html-button-through-javascript-in-android-webview – Dan Dec 28 '13 at 01:46
  • I'd put a bounty on this if I had enough points to do so. Is there any insight on how to accomplish this? – Dan Jan 03 '14 at 16:06

1 Answers1

0

I think you're looking at this wrong. Instead of detecting the double tap in Android and trying to inject it into your Javascript you could be detecting the double tap in Javascript. Just keep the time of the last tap and if it's been recently enough the second tap is part of a double tap.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • I've seen examples of this so I think I know what you're talking about. If I handle the tapping in Javascript, would that allow me to call specific functions defined in the website java for when a person taps and double taps on an element? Or, would that even be necessary? If the double tap occurs on a non-element, nothing would happen but if it occurred on the element, the "ondblclick" function would trigger? – Dan Jan 07 '14 at 19:46
  • It all depends on how you want it to work. If you only want double tap to work on an element then only attach your doubletap listener to the elements. If you want it to work in blank space you can attach it to the body or document. – CaseyB Jan 07 '14 at 20:44
  • Okay, I'll look into this tonight and see if I can find some examples and get it to work. I think I'll need it to work throughout the entire webview. That way I won't have to define all possible double click variables. – Dan Jan 07 '14 at 20:59
  • As I look further into this, I'm starting to believe that I need to be using the OnClickListener and not the OnTouchListener. There's a question [here](http://stackoverflow.com/questions/11231296/a-difference-in-to-ways-of-handling-clicks?rq=1) where an answer indicates a case scenario for the onClick event. I think that would work for me except all these examples are calling buttons by ID - created by the coder - which doesn't work for me. The accepted answer in that link is what I am looking at. – Dan Jan 08 '14 at 07:40
  • You're not trying to get the clicks in Android, right? You want to detect a double tap inside of your Javascript? So you don't need any of these. No touchlisteners or clicklisteners. Just build the logic into your javascript. – CaseyB Jan 08 '14 at 14:31
  • I'm not sure I understand what you're saying here. Could you provide an example or help me understand what you mean by building the logic into my javascript? – Dan Jan 08 '14 at 17:31
  • Are you talking about something like this: WebSettings ws = wv.getSettings(); ws.setJavaScriptEnabled(true); wv.addJavascriptInterface(new Object() { public void performClick() { // Deal with a click on the OK button } }, "ok"); – Dan Jan 08 '14 at 17:46
  • No, just `ws.setJavaScriptEnabled(true);` and then handle the touches in Javascript like you would for any other webpage. – CaseyB Jan 08 '14 at 17:50
  • By handling them in Javascript, do you mean the javascript within the website I am loading or the javascript in the app I am writing? – Dan Jan 08 '14 at 22:44
  • I must not have mentioned this in the question - I do not own the website. I am simply accessing it via webview and therefor can not alter it. – Dan Jan 09 '14 at 23:57
  • If they take care of the double click then it should just work in your webview. Does it? – CaseyB Jan 10 '14 at 15:25
  • If I have nothing in my app except the webview - no gesturedetectors or clicklisteners - the single taps are picked up and the single click function is called. Double-tapping will zoom inherently in android so, no, it will not. And adding the zoom controls (pinch) removes the doubletap-to-zoom but double tapping an element does nothing. – Dan Jan 11 '14 at 05:04