Table of Contents
ToggleHow To Create Webview In Android Studio?
Write In OnCreate():
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new myWebViewclient()); // to handle URL redirects in the app
mWebView.getSettings().setJavaScriptEnabled(true); // to enable JavaScript on web pages
mWebView.getSettings().setGeolocationEnabled(true); // to enable GPS location on web pages
mWebView.loadUrl("https://www.GOOGLE.com");
After OnCreate():
public class myWebViewclient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
final String urls = url;
if (urls.contains("mailto") || urls.contains("whatsapp") || urls.contains("tel") || urls.contains("sms") || urls.contains("facebook") || urls.contains("truecaller") || urls.contains("twiter")) {
mWebView.stopLoading();
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse(urls));
startActivity(i);
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}