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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With