3

I've developed a small group of .html pages that are stored in a server. My android app, using a webview with: setWebChromeClient loads these pages. minSdkVersion 19 targetSdkVersion 24

The problem: Everytime I need to load a new page, using a link in my .html page, the new page is opened in an external browser.

Url Overriding I know about the shouldOverrideUrlLoading() method, when we're using the setWebViewClient(). But unfortunately I can't use the normal webview. I need to use the WebChromeClient() because of some feature that only work with this one. (Like the input file)

My doubt is... How can I override my URL to force them to load inside of the webChromeClient? I tried this but with no luck:

webView.setWebChromeClient(new WebChromeClient() {
        // (...)
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    });
Echeverrya Avila
  • 63
  • 1
  • 2
  • 9
  • WebChromeClient doesn't contain the `shouldOverrideUrlLoading` method so you achieve this the way you are doing. Please check my answer. – Geek Mar 30 '17 at 08:01
  • What do you mean by : `But unfortunately I can't use the normal webview. I need to use the WebChromeClient() because of some feature that only work with this one` ? – Yash Mar 30 '17 at 08:11
  • Hi, because using the setWebViewClient(), the input type "file" in my .html page, to upload an image, doesn't work. It doesn't open the file chooser. When I use the setWebChromeClient() it does. It opens normally. – Echeverrya Avila Mar 30 '17 at 12:16

2 Answers2

4

You can use following code to achieve this.

WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.    

web.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onReceivedTitle(WebView view, String title) {
         getWindow().setTitle(title); //Set Activity tile to page title.
    }
});

web.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});
Geek
  • 1,510
  • 14
  • 17
  • It works great! But when I use the setWebViewClient(), the input type "file" stop working. It doesn't open the file chooser for uploading an image. This feature seems to work fine with setWebChromeClient(). Is there a way to accomplish both things? (Link inside of the webview and also the input file), or maybe a way of change the webview based on the URL I've clicked. Thank you very much for your time! – Echeverrya Avila Mar 30 '17 at 12:29
  • Yes, absolutely! I still have a problem about the file input. It doesn't work when I set the WebViewClient() – Echeverrya Avila Mar 31 '17 at 19:03
  • Yes, unfortunately it doesn't work. I have a button "add service"... When clicked it navigates to a new page, with a form and a button to upload images. When I use the WebChrome, the link open inside of the webview (Your solution works great here!) But the input file strop working, because I've used setWebViewClient()... And this 'normal' webview, doesn't accept the input file. :( – Echeverrya Avila Mar 31 '17 at 19:25
  • Thank you very much my friend! I solved the problem. Using this:https://github.com/delight-im/Android-AdvancedWebView – Echeverrya Avila Mar 31 '17 at 19:46
  • @Wini, `webview.canGoBack()` and `webview.goBack()` to achieve intended behaviour. – Geek Nov 18 '20 at 10:49
0

I was using an embedded mobile web application in an Android App making use of the WebChromeClient class and did not want to fiddle around with recompiling the APK.

While looking for the easiest header(); solution (since the mobile web application is in php) I found out that using:

header("Location: url.php", TRUE, 307);

was a quick fix solution without recompiling the Android app.

This allowed the user to re-direct within the app without calling the web browser externally from my app.

Here is the link to the answer by Arindam Nayak where I got the idea from.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jonas
  • 1,105
  • 1
  • 15
  • 21