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! :)
Related
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
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.
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
Currently I have an Android app that is basically a web view loading a web page. On the web page I've tryed to link to the market like this...
https://market.android.com/details?id=com.google.earth
http://market.android.com/details?id=com.google.earth
market://details?id=com.google.earth
The first result just opens up a white screen (It may be loading, but it has been their for over a minute).
The second result says that the page has been moved and a link. If you click th link it does what the first one did.
The third result says that the page may be temporarily down. (It's treating the link like its online rather than in the phone itself)
Here is how the link looks...
echo "<a href='https://market.android.com/details?id=com.google.earth' data-role='button'>Upgrade Now</a>";
Remember the web page I'm loading is using JQuery Mobile and I'm echoing the link with php.
How can I open a link to the Android Market in a webview on a web page?
In my case, I was getting the same blank/moved temporarily trouble described. I wanted to use the shouldOverrideUrlLoading so that the native browser wasn't used in an oauth2 flow from my page to google and back to my page. My android app was talking to localhost/tomcat with a self-signed cert. Turned out I needed to call proceed because of cert mismatch:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("DevAgentSocialMain", "URL: " + url);
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("DevAgentSocialMain", "onReceivedError: " + errorCode + " " + description + " url: " + failingUrl);
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.i("DevAgentSocialMain", "onReceivedSslError: " + error.getPrimaryError());
//super.onReceivedSslError(view, handler, error);
handler.proceed();
}
});
You can use a callback when a user clicks a link inside the webview.
See
Handling Page Navigationsection on the android developer platform.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);
Then your callback
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
I'm new in android development and I am trying out the WebView example in the official android site.
http://developer.android.com/guide/tutorials/views/hello-webview.html
But I do everything they say...which is pretty simple: I create the project, edit the layout file, then I add the code, etc. No problems building...but when I launch the app in the simulator I just got a black screen. It is like if the Layout is empty...like if the WebView is not created.
What am I doing wrong?
Sorry about that – that link is a bit outdated. The fixed version of this tutorial is available here:
http://developer.android.com/guide/webapps/webview.html
We should remove the old link; I'll file a bug.
And note, the error is that setContentView isn't being called.
in oncreate method add WebView.enablePlatformNotifications();
in manifest file add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
now it works fine...
webview_id = (WebView)findViewById(R.id.webview_id);
webview_id.getSettings().setJavaScriptEnabled(true); // enable javascript
WebSettings webSettings = webview_id.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webview_id.setInitialScale(90);
webSettings.setLoadWithOverviewMode(true);
webview_id.requestFocusFromTouch();
webview_id.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Snackbar.with(getApplicationContext()).dismiss();
Snackbar.with(getApplicationContext()) // context
.text(description) // text to display
.show(MainActivity.this);
}
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
if(isNetworkAvailable()){
webview_id .loadUrl("http://helloworld.org/");
}else{
Snackbar.with(getApplicationContext()).dismiss();
Snackbar.with(getApplicationContext()) // context
.text("Please Check your Internet Connection") // text to display
.show(MainActivity.this);
progressBar.setVisibility(View.VISIBLE);
}
}