Android WebView shows a blank page - android

Trying to create a WebView but it only shows a blank/white page. I have followed several examples and they all say that work with this code...
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class PostenWebView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
WebView webview = (WebView)findViewById(R.id.webview);
webview.loadUrl("http://www.google.com");
}
}
And here is the web_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

You need to enable Javascript (.getSettings().setJavaScriptEnabled(true)), or choose a Web page that does not rely upon Javascript.

You have to add the permission to your AndroidManifest.xml file.
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>

Use webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); method

It's works fine for me
WebView webView = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webView.loadUrl("http://www.google.com");
Good Luck!

You might be getting redirected..
just install a webviewclient allong with you web view :)

in my case loading google.com worked but my login page returned a completely white page. Adding this fixed it:
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
String databasePath = webView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
webSettings.setDatabasePath(databasePath);

The quick and dirty solution can be when your android app get started set the background colour according to web view eg i am using black colour so
myWebView.setBackgroundColor(Color.parseColor("#000000"));

if you try to load a HTTPS URL (e.g. Foursquare authentication URL) do not forget to call
webview.clearSslPreferences();
before trying to load that

Related

Android 7.0 shows blank screen when trying to open url in webview

Im facing one issue in Android 7.0 (moto g4) where im trying to load one url in the webview but it shows a blank white screen.It is working with Android M and below.
public class MainActivity extends Activity {
private String webviewURL="http://henmilholidayhomesgoa.com/player2/documentation/EULA.html";
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv= (WebView) findViewById(R.id.webview);
wv.getSettings().setTextZoom(60);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(webviewURL);
}
}
NOTE:- The url mentioned in the code is not the actual url which unfortunately i cant share :(.Its an url with https. But the given url in the post actually working ,Please let me know if any other input are needed.Thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</RelativeLayout>
Try this sample code:
In this there is a webview and a progress bar.In this code url will be loaded as soon as progress bar disappears and if there is any link within the url it will open it as well in the webview.
Below is the java code:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ShowWebView extends Activity {
//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
startWebView("http://www.androidexample.com/media/webview/login.html");
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(ShowWebView.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
Now below is the respective xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Please check this code in android 7.0. Hope it helps you out !
I have copied your same code and run the application on nougat Nexus 9 .
It take time to load the URL wait for 5 minutes otherwise check with other device of Nougat.
public class WebviewMainActivity extends AppCompatActivity {
private WebView wv;
private String webviewURL = "http://stackoverflow.com/questions/41425766/android-7-0-shows-blank-screen-when-trying-to-open-url-in-webview";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview_main);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setTextZoom(60);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(webviewURL);
}}
Hope this help.Happy coding.Please let me know if it was helpful.
I had similar issue on Asus Nexus 7. Webview version was 56 while Chrome browser app had lower version. After I updated Google Chrome app webview started working fine.
You can use loadDataWithBaseURL instead of loadData and try, I was facing similar issues on some devices.

Getting net::ERR_UNKNOWN_URL_SCHEME while calling telephone number from HTML page in Android

I am getting "net::ERR_UNKNOWN_URL_SCHEME" while calling a telephone number option from an HTML page in Android. Do I need to add any permission(s) in the manifest to get this working? I haven't added anything in the manifest so far. Here's the HTML Code:
Call us free!
The following should work and not require any permissions in the manifest (basically override shouldOverrideUrlLoading and handle links separately from tel, mailto, etc.):
mWebView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle things like tel, mailto, etc.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}
});
mWebView.loadUrl(url);
Also, note that in the above snippet I am enabling JavaScript, which you will also most likely want, but if for some reason you don't, just remove those 2 lines.
I had this issue occurring with mailto: and tel: links inside an iframe (in Chrome, not a webview). Clicking the links would show the grey "page not found" page and inspecting the page showed it had a ERR_UNKNOWN_URL_SCHEME error.
Adding target="_blank", as suggested by this discussion of the issue fixed the problem for me.
Try this way,hope this will help you to solve your problem.
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MyActivity.java
public class MyActivity extends Activity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
webView.loadData("Call us free!", "text/html", "utf-8");
}
}
Please add this permission in AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>

Hide the browser address bar in Android

I've created a WebView app and everything works fine.
I'm new to Android.The only remaining feature I need for the app is to hide the address bar. Because I want the app to look more like a regular app and not a webpage within a web browser window.
My code is like this,
package com.Mobi.ebookread;
public class Mobile extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webview = (WebView) findViewById(R.id.helloWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
}
}
How do I do it?
i think there is a problem. webview does not have an address bar, seems that the browser app is getting opened. It could be possible that there is a redirection happening and that causes the browser app to open up, and you did not intercept that redirect using a WebViewClient and shouldOverrideURLLoading()
Below is how the webview looks like.
Finally I Try with this. Its worked for me..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//webview use to call own site
webview =(WebView)findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient()); //use to hide the address bar
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true); //to store history
webview.loadUrl("http://www.google.com");
}
and your entire main.xml(res/layout) look should like this:
`<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>`
don't go to add layouts.
try the following code:
webView=(WebView) findViewById(R.id.helloWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://www.google.com");

detecting when phonegap webview finishes loading android

Does PhoneGap (more specifically DroidGap for Android) have way to tell when its webview has been loaded. I'd like to find something like the following for PhoneGap:
Normal WebView Client Creation:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
Creating my PhoneGap Webview like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
if (isOnline() == true) {
WebSettings settings = mWebView.getSettings();
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://myurl.com");
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
}
});
}
else {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
Intent myIntent = new Intent(getContext(), LoadScreen.class);
startActivity(myIntent);
finish();
}
}
My Main_Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/splash"
/>
<WebView android:id="#+id/mWebView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>
DroidGap does not have a method, since it's an activity, and not a view. Instead, the CordovaWebViewClient uses its onPageFinished method to send a message to all the plugins when the page has finished loading. This is what plugins can use to determine whether the page has finished. Conversely, you could check for the deviceready event in Javascript, which should be fired after the page has been loaded.
If you need to know when the WebView has loaded in Java for anything other than plugins, I highly recommend switching to using CordovaWebView and implementing the lifecycle events yourself, since this will give you more control over the augmented view. This also means that you have to make sure that plugins which use activities are handled correctly, otherwise they will break.

Enabling general JavaScript in WebViewClient

While searching for an answer in google, it seems that I'm not the only one stuck with a problem that seems impossible to solve.
I've managed to create a WebView with a custom WebViewClient - this makes it possible for me to have a processdialog and show an error message if an URL couldn't be loaded.
But this creates a problem with JavaScript. The URL I'm loading has some JavaScript which changes some HTML elements CSS styles (showing or hiding element) or redirects to another location onclick - or maybe even want's to show an alert box. But by using the WebViewClient none of those are working.
This is how I load a page:
public void loadUrl(String url)
{
final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", getString(R.string.loading), true);
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setVerticalScrollBarEnabled(false);
myWebView.setHorizontalScrollBarEnabled(false);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes
if (url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/mp4");
view.getContext().startActivity(intent);
}
else
{
view.loadUrl(url);
}
return true;
}
public void onPageFinished(WebView view, String url)
{
//Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show();
String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>";
myWebView.loadData(summary, "text/html", "utf-8");
}
}); //End WebViewClient
myWebView.loadUrl(url);
}
This could probably be done in a smarter way, but I'm new at both Java and Android development...
Is it possible for me to enable the JavaScript for the WebViewClient at all?
Removing the WebViewClient solves the problem, but then I can't catch events when the page errors or is finished loading.
I don't know what your exact problem is, but i can enable the JavaScript and a custom WebViewclient without any problem:
WebView vistaWeb = (WebView) findViewById(R.id.webview);
vistaWeb.setWebChromeClient(new MyCustomChromeClient(this));
vistaWeb.setWebViewClient(new MyCustomWebViewClient(this));
vistaWeb.clearCache(true);
vistaWeb.clearHistory();
vistaWeb.getSettings().setJavaScriptEnabled(true);
vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
The proper way to enable JavaScript is by add the below two lines:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Even if after adding its not working then try adding the below line.
mWebView.getSettings().setDomStorageEnabled(true);
Now It should work. :)
Try this to enable javascript
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl(url);
What happened in my case : I was serving local html files and when applying
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
but then my urls stopped working. Solution to make both JS and local href work was to add
web.getSettings().setAllowFileAccess(true);
web.getSettings().setAllowFileAccessFromFileURLs(true);
where
web = (WebView) findViewById(R.id.embedded_web);
-> Hello Please Try This Code This Code Run Fine
-> For More Information of Documentation Please Visit https://developer.android.com/guide/webapps/webview#java
-> This Link Work Fine in Nov 2019 Currently I Don't Know
-> Your XML Code For Design is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
-> And Your Java Code For Backend is
package com.developer.harshil.kaneria.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://github.com/Harshil-Kaneria");
}
}
-> Here in Java Code
WebSettings webSettings = myWebView.getSettings();
And This
webSettings.setJavaScriptEnabled(true);
-> It is Allow To JavaScript Run When Page is Load
-> Thanks A Lot For Read.
Do "Javascript-URLs" get routed through shouldOverrideUrlLoading? Try checking that and if so, return false for links like that (so that the webview handles the link, not your WebViewClient)
Similar to #mdelolmo answer, but in Kotlin:
webview.setWebChromeClient(WebChromeClient())
webview.setWebViewClient(WebViewClient())
webview.clearCache(true)
webview.clearHistory()
webview.getSettings().setJavaScriptEnabled(true)
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true)
How enable programmatically answered in other answers. But some javascripts not worked if your webview is in nestedscrollview. Remove it and add webSettings.setJavaScriptEnabled(true);
This code happen to works for me, I hope it can helps you and other developer:
webview = findViewById(R.id.WebView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.loadUrl("www.example.com");

Categories

Resources