Fail to open link in webview in android. I tried it in webview but it only shows blank page. It seems that some of urls are loaded but some not how is it.
String strUrl = "https://app.mygtechguy.com/5862bd814bf7f";
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(getActivity(), "Oh no! ", Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage("Make sure internet is working well");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().getAllowContentAccess();
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(strUrl);
I would suggest try to remove the url loading from shouldOverrideUrlLoading like this:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//view.loadUrl(url);
return false;
}
And also in the AndroidManifest.xml set Internet permission:
<uses-permission android:name="android.permission.INTERNET" />
Related
I'm trying to load an url that fit security protocols with HTTPS, but when I'm trying to load on a WebView, android shows me net::ERR_CLEARTEXT_NOT_PERMITTED. Why? is a HTTPS what is the problem?
The source code that shows it is:
public class InternalWebBrowserActivityHelperImpl implements InternalWebBrowserActivityHelper, Constants {
private final String TAG = getClass().getSimpleName();
#NonNull
private InternalWebBrowserActivityView activityView;
public InternalWebBrowserActivityHelperImpl(#NonNull InternalWebBrowserActivityView activityView){
this.activityView = activityView;
}
public WebChromeClient getWebChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
};
public WebViewClient getWebViewClient() {
return new WebViewClient() {
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LoggerManager.handlesError("onReceivedError", request.getUrl().toString());
}else{
LoggerManager.handlesError("onReceivedError", error.toString());
}
activityView.hideLoadingView();
activityView.showWebView();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
LoggerManager.handlesError("onPageFinished", url);
activityView.hideLoadingView();
activityView.showWebView();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//activityView.showLoadingView();
LoggerManager.handlesError("override", url);
return super.shouldOverrideUrlLoading(view, url);
}
};
}
#Override
public void setupHelper(String url) {
//7activityView.showLoadingView();
activityView.showWebView();
WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.FAR;
activityView.getFullWebView().getSettings().setJavaScriptEnabled(true);
activityView.getFullWebView().getSettings().setDomStorageEnabled(true); // Add this
activityView.getFullWebView().getSettings().setDefaultZoom(zoomDensity);
activityView.getFullWebView().getSettings().setSupportZoom(true);
activityView.getFullWebView().getSettings().setBuiltInZoomControls(true);
activityView.getFullWebView().requestFocus(View.FOCUS_DOWN);
activityView.getFullWebView().setWebChromeClient(getWebChromeClient);
activityView.getFullWebView().setWebViewClient(getWebViewClient());
activityView.getFullWebView().loadUrl(url);
}
}
Thanks
Are you sure the URL you give to your webview is in HTTPS ?
Otherwise a quickfix would be to add the below line in your application as shown below:
<application
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_config"
....
</application>
I strongly advise that you create the network_security_config to only allow your domain and subdomain. Here is a quick tutorial
i am trying load a .php file in WebView. it was working properly before API level 28 after i updated to API level 28 its not working its showing white screen. nothing is showing i tried all the options.
Here is the code
I am added the safe browsing false in the manifest file also.
I tried searching in the google nothing is helped me if any one done please help in this case to resolve.
String url="https://xxx/xxx/abc.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleSSLHandshake();
WebView mWebView = (WebView) findViewById(R.id.webView);
// PackageInfo webViewPackageInfo = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// webViewPackageInfo = WebView.getCurrentWebViewPackage();
mWebView.getSettings().setSafeBrowsingEnabled(false);
// Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);
}
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
mWebView.setWebViewClient(webViewClient);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
// Log.i(TAG,url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
I solved my problem. it was the certificates issues, i just added the following code, its working fine now
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
This is the code part where I'm having the issues...
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("tel:") > -1) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
} else {
return true;
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:replace('window.location.href', 'window.location')");
view.loadUrl("javascript:replace('document.location.href', 'document.location')");
}
});
I'm just adding on shouldOverrideUrlLoading the functionality to the webview to make calls when the link is a phone number, and adding some javascript code on onPageFinished to fix some issues that the webview has with window.location.href and document.location.href...
Thanks in advance for all the comments and replies, fixes and alternatives are welcome.
You can make new class which extends which extends WebViewClient.
Try this code snippet
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
}
Then used in your onCreate method.
webView.setWebViewClient(new MyWebViewClient());
In this both function are calling.CheckOut this.
Dhanyavaad!
My problem was in my shouldOverrideUrlLoading. I was returning true when the link is NOT a phone number, now if the url is not a phone number, returns super.shouldOverrideUrlLoading(view, url); and works perfectly.
Thanks Shashank your code open my eyes.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("tel:") > -1) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
}
});
I have some web page which open in WebView.
<body onload="window.location.href='htcmd:loaded';">
After load we open back url "htcmd:loaded" and intercept in code.
Like this:
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("htcmd:loaded".equals(url)) {
Toast.makeText(getActivity(), "htcmd:loaded", Toast.LENGTH_SHORT).show();
}
return true;
}
});
getWebView().loadUrl("https://some.url");
On android 4.4.2 in first start all is well. But if I kill app and open after first run, web page not render. But if I tap on screen or change orientation web page appears. Where is problem?
SOLUTION: I have two hacks)))
First: add a java script to web page:
<body onload="setTimeout(function(){window.location.href='htcmd:loaded';},3000);">
Second: add code to web client:
#Override
public void onPageFinished(WebView view, String url) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
view.requestFocus();
}
}
try this
w.getSettings().setLoadWithOverviewMode(true);
w.getSettings().setUseWideViewPort(true);
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
});
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
getWebView().loadUrl("https://www.google.com");
I have loaded an url in WebView in android, when i click a link in that loaded url in WebView , the link loaded in WebView ugly, to remove this problem I had used setIntialScale(50) , this made my initial url becomes small
Works like a charm.
Check it out
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
I am not sure but i think you need to implement WebViewClient.
For example:
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
alertDialog.setTitle("Error!! Something went wrong");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
When you click the link, the phone is opening the new page in the browse instead of your webview. If you disable Url Overriding all subsequent pages will load in the webview and preserve the zoom.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});