Skip to content Skip to sidebar Skip to footer

Clickevents Are Not Working In Web View Android

I have to create we application in android. So what i done is that,simply created raw folder under res and put html files there. All works fine but when i click a button that is pu

Solution 1:

I am doing this type of things using phonegap. If you want to call native functions, use java script enabled web view. I am extends DroidGap in my MainActivity and my Login.html file under assets folder in your Main Activity.java,

WebView webView=newWebView(this);
webview.loadUrl("file:///android_asset/www/Phone/Login.html");
setContentView(webView);

Anyway, if problem not solved, try adding this (all codes in onCreate method)

    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setSupportZoom(true);
    webView.setWebViewClient(new WebViewClient());
    webView.setClickable(true);
    webView.setWebChromeClient(new WebChromeClient());

If you want more about how to access Android native code through webpage here is the link

Solution 2:

When a WebView doesn't respond to a button, you can test a web page inside a mobile browser or a desktop browser with minimum width (adaptive layout). You will see how the button works when you click it. Then you can press F12 and see what queries are sent in "Network" tab.

enter image description here

(If you press Ctrl+Shift+C, you will see a layout of the page, then can click an element (the button that you look for) and see a code.)

If the button shows a picture dialog (take a photo, upload a picture), you should use onShowFileChooser ([1]).

val startActivityForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
    //
}

@SuppressLint("SetJavaScriptEnabled")overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    with(webview) {
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true// webViewClient = MyWebViewClient()
        webChromeClient = MyWebChromeClient()
    }
}

privateclassMyWebChromeClient : WebChromeClient() {

    overridefunonShowFileChooser(
        webView: WebView?,
        filePathCallback: ValueCallback<Array<Uri>>?,
        fileChooserParams: FileChooserParams?
    ): Boolean {
        // Check permissions, create intent.
        startActivityForResult.launch(chooserIntent)
        returntrue
    }
}

In case you have a JavaScript button, you can attach JavaScript interface ([1], [2], [3]):

@SuppressLint("SetJavaScriptEnabled")overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    with(webview) {
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true// WebView.setWebContentsDebuggingEnabled(BuildConfig.TEST)
        addJavascriptInterface(
            JsHandle(this@WebViewFragment),
            "Android"
        )
    }
}

classJsHandle(privatevar fragment: WebViewFragment) {

    @JavascriptInterfacefunnotify(notify: String) {
        //
    }
}

Your web page should have javascript: inside.

Post a Comment for "Clickevents Are Not Working In Web View Android"