Android WebView Not Loading Correctly - android

I'm having a few issues regarding the WebViewClient on Android.
The site works perfectly on any mobile browser. Including the ChromeViewClient that I have set for debugging purposes.
And the website that I am loading does not have any issues or errors when using any other mobile browser. Using Chrome's inspector and selecting a device, using as mentioned the native Android browser and also tested on an iOS WebView Component to make sure.
The WebViewClient renders "parts" of the website. Images on one page and not the other, buttons that can not be clicked, a slider that does not work, etc. The website that I am loading is very JavaScript and HTML5 intensive. I am completely out of ideas of how to debug this issue further, are there certain JavaScript libraries that the WebViewClient can't load properly? Is there any other method you would recommend I implement while trying to debug this issue? Or am I missing some really small thing that will make me hit my head against the table?
These are the JS files we are using on the website:
bootstrap.min.js;
jquery.min.js;
swiper.jquery.min.js;
slideout.min.js;
owl.carousel.min.js.
Code for the WebView:
this.webview = (WebView)findViewById(R.id.webView);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
firstLoad = true;
return true;
}
// when the page is finished loading
public void onPageFinished(WebView view, String url){}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getBaseContext(), "Could Not load. " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("mywebsite.com");

I got it working by setDomStorageEnabled(true);
You need to set this when using local storage.

Related

Android. WebView displaying white page

Why the web view can show a white page, although the page opens in the browser. At first there was an error:
net::ERR_CACHE_MISS
but after I added this code:
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
there are no errors, but the page does not load, just a white page is displayed. The page opens by clicking on the button inside in webview.Clicking is not processed on the mobile application side.
My manifest also has permission:
<uses-permission android:name="android.permission.INTERNET" />
Please help me. I don’t understand why this can happen. The page also opens successfully on iOS.
Here is my code:
private void initView(String url) {
wvDetail.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("webview://closeScreen")) {
activity.finish();
return true;
} else
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(activity, description, Toast.LENGTH_LONG).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
wvDetail.getSettings().setJavaScriptEnabled(true);
wvDetail.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wvDetail.loadUrl(url);
}
It seems to be a problem with the settings of your webview. Try and add the following settings:
WebSettings settings = wvDetail.getSettings();
//if your page needs javascript
settings.setJavaScriptEnabled(true);
//to handle your cache
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCacheEnabled(true);
settings.setAppCachePath(cacheDir.path);
To enable some functionalities of JS you should also use setdomStorageEnabled (see this question):
settings.setDomStorageEnabled(true)
If your page is still not loading and you fully trust the web page that you will load, I would also use this settings to give even more access to the webview:
settings.setBlockNetworkImage(false);
settings.setLoadsImagesAutomatically(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
settings.setSafeBrowsingEnabled(false);
}
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
To load the webview with the settings of the meta tag, use:
setUseWideViewPort(true);
Finally you could check upon your webView size and make it fit on your system window like this:
wvDetail.setFitsSystemWindows(true);
Just like a bonus if you are targetting above API level 11, you could set your layer type like this:
wvDetail.setLayerType(View.LAYER_TYPE_HARDWARE, null);
I hope this explanation of the common settings used in a webview makes sense! Depending on your webpage I think that some of them can be useful to you! let me know if it works! :)

Javascript on Android WebView not working

I have some issues with Android WebView and Javascript.
Some of customers of app said that WebView on app is not showing anything.
As I checked - its probably not showing javascript at all (whole webpage is loaded in javascript by react).
That my code:
public void setupWebView(WebView accessWebView) {
accessWebView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
handleRedirect(accessWebView);
return true;
}
});
accessWebView.getSettings().setJavaScriptEnabled(true);
accessWebView.getSettings().setDomStorageEnabled(true);
accessWebView.loadUrl(URL);
(I have to use WebViewClient, not WebChromeClient, because of the redirect handling)
Is there anything possible to change so the javascript will load on EVERY device with Android +5.0?
Is it possible that updating WebView on device will help some users?
You need to use setWebChromeClient to enable javascript in your WebView. But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time. Just like in official docs:
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("https://developer.android.com/");
https://developer.android.com/reference/android/webkit/WebView.html

Javascript not working in Android webview

I'm new to Android Studio, and have run into a problem that I can't fix.
I'm trying to run a HTML file in a webView, but for some reason it keeps reloading to my startpage/index file, when I try to call specific HTML that includes a specific JS file.
Note: these files work fine in every desktop and mobile browser, and works on UIWebView in an Apple App.
My MainActivity looks like this:
MyWeb = (WebView) findViewById(R.id.myWebView);
MyWeb.setWebViewClient(new WebViewClient())
MyWeb.getSettings().setJavaScriptEnabled(true);
MyWeb.getSettings().setSaveFormData(true);
MyWeb.getSettings().setDomStorageEnabled(true);
MyWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
MyWeb.loadUrl("URL");
If I remove the "MyWeb.setWebViewClient(new WebViewClient())" part, the file works, but the app launches a browser with an address bar.
Please help - I've allready spend more days trying to fix this, than I care to admit.
As you have already enabled necessary settings, try doing this once:
MyWeb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
MyWeb.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
In order for it to not launch in a browser you must add the line
MyWeb.setWebChromeClient(new WebChromeClient());

Webview doesn't show a specific website

I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST

reCAPTCHA Not Rendering in Android Webview

I have an Android app that is redirected users to a webpage that contains a reCAPTCHA question. Previously, I was implementing this simply by opening a browser window, and directing the user there. Recently, I changed it to use a webview instead for a better user experience, but the problem is that now for some reason the reCAPTCHA question is not rendered on the page; everything else functions normally. Why would this be, and how might I fix it? I assume this must have something to do with accessing a different domain from the webview (www.google.com), but not sure how to configure things differently that it's not an issue. Here is how I am setting up the Webview. Note that the overridden method is for handling some OAuth authorization process that can happen in this Webview. Even if I comment that out, I have the same problem.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl(this.url);
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url){
Uri uri = Uri.parse(url);
if (url != null && uri.getScheme().equals(NNApplication.CALLBACK_SCHEME)) {
SharedPreferences shPref = getSharedPreferences("NN_PREFS", Activity.MODE_PRIVATE);
new OAuthAccessTokenTask(Authorization.this, consumer, provider, shPref).execute(uri);
webView.setVisibility(View.GONE);
finish();
return true;
}else{
return false;
}
}
#Override
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed() ;
}
});
Likewise, you can view the reCAPTCHA question at the URL below. I already checked, and it's behaving the same between our development site and our live site:
https://www-dev.usanpn.org/user/register
Captcha requires javascript:
myWebView.getSettings().setJavaScriptEnabled(true);

Categories

Resources