I have an android application that I am developing using the emulator running on android 2.3.3 with an embedded WebView in a framelayout nested in a linearlayout (vertical).
I gave +1 because these were both valid assistance to getting the webview to working properly, but it was not the solution to my issue.
What I didn't realize is I had a wrap_content on the include of my webview layout and this caused it to have a height of 0px. By setting this to fill_parent I resolved the issue. (Webview with white background and app with white background you cant really tell its not full height when it shows up as full height in the layout designer.)
Thanks for your help though!
I think What you need is WebViewClient. After setting webviewclient call webview.loadUrl(_url); Somethin like this ....
private void loadUrlInWebView(String _URL){
WebView myWebView = (WebView) findViewById(R.id.webviewer);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl(_URL);
}
private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
Try increasing the RAM for your emulated device. That solved the problem for me.
I found this out by looking at the logcat messages in Android Monitor (Android Studio). The messages below gave me the idea about what was wrong to increasing RAM :
W/art: Throwing OutOfMemoryError "Failed to allocate a 6635532 byte allocation with 1170528 free bytes and 1143KB until OOM"
W/JavaBrowserViewRendererHelper: Error allocating bitmap
E/chromium: [ERROR:java_browser_view_renderer_helper.cc(132)] Error unlocking java bitmap pixels.
I think if you edit some of your code then it will be ok.Look how i do it in my case below.Try for you in this way..
For displaying a web page
final WebView wbView = (WebView) findViewById(R.id.WebView);
WebSettings settings = wbView.getSettings();
settings.setJavaScriptEnabled(true);
wbView.loadUrl("https://play.google.com/store/apps");
wbView.clearView();
wbView.measure(100, 100);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
For showing HTML:
See this tutorial..
clearView
is deprecated.
Use:
webView.loadUrl("about:blank")
You can use the following code, this will be helpful:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
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;
}
}