Zoran
Zoran

Reputation: 1494

Webview not catching javascript touch events

I have a web view which loads url. When url loads, there should be a reaction on touch events like touchstart, touchmove and touchend. If i try it on web browser on device alerts are fired, but if i open it in webview, there is no reaction. Am i missing something?

Here is my code:

webview.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutBoard"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/wvBoard"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:clickable="false"
        android:scrollbars="none" />

</RelativeLayout>

Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.webview);

    myWebView = (WebView) findViewById(R.id.wvBoard);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(my_url);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(false);
    myWebView.setHorizontalScrollBarEnabled(false);
    myWebView.setVerticalScrollBarEnabled(false);
    myWebView.getSettings().setLayoutAlgorithm(
        LayoutAlgorithm.SINGLE_COLUMN);

    myWebView.setInitialScale(170);
}

javascript on serverside:

$(document).ready(function () {
    ...

    canvas.addEventListener('touchstart', ev_canvas, false);
    canvas.addEventListener('touchmove', ev_canvas, false);
    canvas.addEventListener('touchend', ev_canvas, false);
}

function ev_canvas(ev) {
    this.touchstart = function (ev) {
        alert("called" + ev._x);
    };   
}      

Edit: I tried adding:

webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
    return super.onJsAlert(view, url, message, result);
    }
});

but nothnig changed.

Edit2: I noticed one line in logcat which sais: nativeOnDraw failed; clearing to background color. It seems this error appears only on Android 4.4 (which is also my case) but solution in this link: WebView Rendering Issue in Android KitKat didn't help.

Upvotes: 1

Views: 3874

Answers (3)

Tyler
Tyler

Reputation: 18157

After being frustrated for awhile, I closed my Android app, then reopened it, and everything was working fine. I am not sure what caused things to stop working, but the ole' turn-it-off-and-on did the trick for me.

Upvotes: 0

Thushara
Thushara

Reputation: 559

This happens at the last time when the webview destroyed with a alert. So when the next time web view open there are two webview instances opened. So when the webview destroyed we should clear one webview instance.

@Override
    public void onDestroy() {
        super.onDestroy();
        webView.loadUrl("about:blank");
}

This was fixed mine.

Upvotes: 1

Sreekumar R
Sreekumar R

Reputation: 194

It need not be an issue with the touch events. It could be that the alerts are not working. Try using a WebChromeClient as suggested in this JavaScript alert not working in Android WebView stackoverflow question

Upvotes: 0

Related Questions