Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicks Does Not Working in WebView

I have a webview in my android app.
I have loaded the url: https://imsnsit.org/imsnsit/notifications.php
There are links to various notifications. My WebView is doing nothing when I click on them. It is working on everything else - Chrome (Android), Chrome(Desktop). The links are fine. One thing I notice notifications have the href with PHP file. Just navigating to that link does not work. I get Invalid operation. Plus the links are not static, they change everytime you refresh the page(only just the parameter for plum_url.php).
I am already using these functions. Nothing helps.
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setDomStorageEnabled(true);

like image 329
Ishaan Kumar Avatar asked May 03 '17 14:05

Ishaan Kumar


1 Answers

I checked your link and all the links are opening a pdf file which is not supported in android web view. you can open such links in google docs or maybe some app in you device. Implement shouldOverrideUrlLoading something like this.

 @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if ( urlIsPDF(url)){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "application/pdf");
        try{
            view.getContext().startActivity(intent);
        } catch (ActivityNotFoundException e) {
            //user does not have a pdf viewer installed
        }
    } else {
        webview.loadUrl(url);
    }
    return true;
}
like image 80
megamind Avatar answered Sep 30 '22 12:09

megamind