Android onTouchEvent and JavaScript onclick events
<p><strong>[Revised]</strong></p>

<p>I did manage to get this to work, but I'm not sure this is the correct way to do it. Comments are welcome.</p>

<p>In the <code>onTouchEvent</code> in the WebView, I pass the event up to <code>super</code> and return <code>true</code> (which prevents the event from propagating further and allows JavaScript to do its thing). But before returning, I also direct the event to the parent container's <code>onTouchEvent</code> -- but I have to cast the container to a class that has <code>onTouchEvent</code> or specify an interface. In my case, the WebView's <code>onTouchEvent</code> now looks like this:</p>

<pre><code>@Override
public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);
    ((RelativeLayout)getParent()).onTouchEvent(ev);
    return true;
}
</code></pre>

<p>It works, but is this a proper solution?</p>

<hr>

<p>I cannot figure out how to have an Android Webview 'share' touch events with both the HTML page (via JavaScript) and the rest of the application. From what I'm seeing, it looks like either JavaScript can see the touches or the WebView's parents can, but not both. Does anyone know a work-around?</p>

<p>Below is a working example with an Activity (TestActivity.java), a RelativeLayout (TestLayout.java) and a WebView (TestWebView.java). The WebView has an <code>onTouchEvent</code> (which declares whether the touch event has been handled) and a simple HTML page. There are three places where touch events are detected:</p>

<ol>
<li>the HTML page (via an <code>onclick</code> around an <code>&lt;h1&gt;</code> -- if you touch it, the page turns color;</li>
<li>the onTouchEvent of the WebView -- if you touch it, a message is logged;</li>
<li>the onTouchEvent of the Layout -- if you touch it, another message is logged.</li>
</ol>

<p>If the WebView's <code>onTouch</code> returns <code>true</code> (signaling that it's "handled" the event), the JS <code>onclick</code> works (the page changes colour) but the parent's <code>onTouchEvent</code> is never reached (which you would expect). If the WebView's <code>onTouchEvent</code> returns 'false', the parent's <code>onTouchEvent</code> is invoked, but the JS never receives the event (strangely a box appears around the word to show that it's been touched, but the JS never executes).</p>

<p>TestActivity.java</p>

<pre><code>package com.scriptfoundry.LinkTest;

import android.app.Activity;
import android.os.Bundle;

public class LinkTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TestLayout layout = new TestLayout(this);
        TestWebView webView = new TestWebView(this);
        layout.addView(webView);

        setContentView(layout);

    }
}
</code></pre>

<p>TestLayout.java:</p>

<pre><code>package com.scriptfoundry.LinkTest;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

public class TestLayout extends RelativeLayout {

    public TestLayout(Context context) {
        super(context);
        setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        setBackgroundColor(Color.LTGRAY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.d("Info","TestLayout onTouchEvent invoked");
        return false;
    }
}
</code></pre>

<p>TestWebView.java:</p>

<pre><code>package com.scriptfoundry.LinkTest;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.Xml.Encoding;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.RelativeLayout;

public class TestWebView extends WebView {

    public TestWebView(Context context) {
        super(context);
        setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT));
        setBackgroundColor(Color.WHITE);
        getSettings().setJavaScriptEnabled(true);
        loadData("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1 onclick=\"document.body.style.backgroundColor=document.body.style.backgroundColor==='red'?'blue':'red'\"&gt;Change colour&lt;/h1&gt;&lt;body&gt;&lt;/html&gt;","text/html",Encoding.UTF_8.toString());
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.d("Info","TestWebView onTouchEvent invoked");
        super.onTouchEvent(ev);
        return false;
    }
}
</code></pre>
