42

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). No matter what code I use it never actually does anything. I have Permissions on the android application set to INTERNET. Even when trying to load just the HTML code. I get a blank white page, no errors no nothing. Nothing in logcat as well.

WebView wview = (WebView)findViewById(R.id.webView1);
wview.getSettings().setJavaScriptEnabled(true);

I have tried all three of these to attempt to load anything into the webview:

wview.loadUrl("http://www.google.com");
wview.loadData("<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8");
wview.loadDataWithBaseURL(null, "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8",null);

All three give me the same results. Nothing.

Any ideas?

16 Answers 16

81
WebSettings settings = wbView.getSettings();
settings.setDomStorageEnabled(true);
7
  • 2
    That was the issue for me. The webview was able to display some web pages, but had a blank screen for some other pages. Adding this solved my problem, thanks!
    – androidseb
    Aug 10, 2017 at 20:41
  • 10
    I had to use both this and wbView.getSettings().setJavaScriptEnabled(true); Mar 13, 2018 at 23:52
  • 1
    save my time...Thank you. Jan 23, 2019 at 6:56
  • 1
    Made my day ! Thank you !
    – Itoun
    Feb 27, 2020 at 13:55
  • Thank you !! Was trying for hours to fix this
    – Subin
    Sep 6, 2020 at 10:38
14

It might be a bit too late, but I wanted to share my experience with you, because I had the same problem and spent a lot of time on it.

Put your WebView in RelativeLayout and not in FrameLayout.

What I have tested: My WebView's onPageFinished() was called every time, but on the screen I got blank page.

From chrome://inspect WebView debugger I was able to "ping" my WebView by pressing inspect button, and after that WebView was refreshing immediately.

My thoughts - WebView isn't rendered correctly in FrameLayout, and RelativeLayout fixes the issue (even invalidate() and requestLayout() calls didn't help).

2
  • Depending on your layout, you also may have to make sure you're setting android:layout_height="match_parent" (instead of wrap_content), as the webApp might just have zero height at first
    – Dani
    Nov 11, 2017 at 16:37
  • I have the same problem and it is already in relativeLayout ... I even set settings.setDomStorageEnabled(true); and I still get a white page on a certain link. The link is even https.
    – Black
    Oct 31, 2019 at 9:13
12

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..

10

instead of passing null, you should pass an empty String to it. ex: ""
So you should call it like this:
wview.loadDataWithBaseURL("", "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8","");

1
  • This actually resolved a blank issue when replacing this overload: .LoadData(html, mime, encoding).
    – CRice
    Apr 20, 2022 at 6:00
6

In my case, it was due to the fact that my webview was attempting to load a page using HTTPS where the certificate was untrusted.

You can check if this is the case in your code using the following snippet:

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed();
}

Java

override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
    handler?.proceed()
}

Kotlin

Then, if it is the case, quickly remove the snippet and refer to the documentation to diagnose the problem further and potentially add the certificate to your keystore.

2
  • This pointed me in the right direction. Annoyingly the WebView component does not make use of certificates added to the system key store. I had to instead include the CA cert in the project itself and use a Network Security Config to trust the self-signed cert during debugging. Nov 9, 2021 at 19:26
  • Thank you very much, it was my problem of untrusted certif
    – ΩlostA
    Mar 29, 2022 at 13:23
5

If it still Doesn't work then please make sure that you have added "http://" or "https://" before the web address.

4

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!

4

clearView is deprecated.

Use:

webView.loadUrl("about:blank")
3

change wrap_content to fill_parent in your webview layout. it worked for me

2

use ip address instead of web url then override webclient using following ssl error handling method

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // Ignore SSL certificate errors
}
1

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();
    }
}
1

just use loadDataWithBaseURL

        loadDataWithBaseURL(null,"html content" , "text/html" , "utf-8",null)
0

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.
0
0

My webview was displaying a white blank square on top of it, sometimes totally blank. This only occurred when it was loading certain data.

Changing it to a RelativeLayout, and setting layout_height to match_parent worked for me.

<RelativeLayout xmlns:android=...
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/ll_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="3dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        .....

    </RelativeLayout>

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ll_title"
        android:background="@color/colorYellow" />


    <ProgressBar
        android:id="@+id/pb_spinning"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

</RelativeLayout>
0

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;
    }
}
0

In my case I was trying to load an html file. The solution came to be:

val webClient = object : WebViewClient() {
    override fun onPageCommitVisible(view: WebView?, url: String?) {
        super.onPageCommitVisible(view, myUrl)
        binding.newsWebView.stopLoading()
    }
}

binding.newsWebView.webViewClient = webClient

binding.newsWebView.loadUrl(myUrl)

After the html file has been loaded, it loaded about:blank and the screen was going blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.