11

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>
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sara
  • 3,733
  • 6
  • 26
  • 30
  • 1
    I am having this problem too. My code: mWebView = (WebView) findViewById(R.id.web_view); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setSavePassword(false); url="http://www.google.com"; mWebView.loadUrl(url); Doesn't show the web page until after an orientation change. Then the page shows up fine. – Richard Jul 07 '10 at 17:07
  • Same problem here. Doesn't show the web page until an orientation change or clicking the webview. Then the page shows up fine. Any solution for this! – user4500 Apr 04 '13 at 13:43
  • It could be that while you want to load www.google.com the actual URL is different(I tried myself www.google.com in the browser and final URL is slightly different). There is a known bug that will not allow a webview to load URL's with parameters. There is also a fix. I could not get it working though. Sorry I can not find the relevant links. I found the problem a while ago. – bboydflo Nov 19 '13 at 12:52

8 Answers8

16

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i am trying to read header of gmail account using http://stackoverflow.com/a/3134609/336990 , but final htmlContent which i am getting not able to load in web view ..showing me blank screen, if same html data file i run on browser it showing me to choose my mail account for allow acceess...why it showing blank screen in emulator – CoDe May 23 '12 at 12:15
  • 8
    The problem doesn't depend on Javascript being enabled – miniBill Sep 26 '12 at 08:32
4

You have to add the permission to your AndroidManifest.xml file.

<uses-permission
        android:name="android.permission.INTERNET"></uses-permission>
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 4
    Alreade done that... I use internet in several other activities in my application and they are working just fine, so that can't be what's making it show a blank page... :/ – Sara Apr 02 '10 at 17:28
  • I don't know anything else other than this. Make sure you have 3G icon the taskbar. If you do this in emulator restart it a couple of times to get it there. – Pentium10 Apr 02 '10 at 17:32
3

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!

GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
jsancheh
  • 54
  • 1
  • 3
2

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

Jaya
  • 999
  • 11
  • 8
0

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

Abhinava
  • 1,030
  • 9
  • 19
0

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);
alex
  • 41
  • 3
-3

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"));

umar
  • 3,073
  • 5
  • 35
  • 45
-4

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

Ayreon
  • 5
  • 3