Reputation: 63
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;
}
});
Upvotes: 3
Views: 5361
Reputation: 1195
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.
Upvotes: 0
Reputation: 1530
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;
}
});
Upvotes: 4