whey i wrote below code, the url is opening in the default browser, why its not loading in the my app.
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.google.com");
whey i wrote below code, the url is opening in the default browser, why its not loading in the my app.
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.google.com");
Try setting a WebViewClient
on the WebView
:
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
myWebView.loadUrl("http://www.google.com");
Maybe you want this solution:
WebView webview = findViewById(R.id.webviewLW);
webview = new WebView(this);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webviewLWComplience.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
webviewLWComplience.loadUrl(CONSTANTS.HOST_URL);
setContentView(webviewLWComplience);
Don't forget to add permission:
<uses-permission android:name="android.permission.INTERNET"/>
Also check this link: https://stackoverflow.com/a/7306176