Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView shows a blank page

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>
like image 381
Sara Avatar asked Apr 02 '10 17:04

Sara


4 Answers

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

like image 91
CommonsWare Avatar answered Oct 22 '22 12:10

CommonsWare


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

<uses-permission
        android:name="android.permission.INTERNET"></uses-permission>
like image 27
Pentium10 Avatar answered Oct 22 '22 14:10

Pentium10


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

like image 24
Jaya Avatar answered Oct 22 '22 13:10

Jaya


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!

like image 33
jsancheh Avatar answered Oct 22 '22 13:10

jsancheh