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?

user1844638
  • 1,067
  • 3
  • 23
  • 48
Chris White
  • 709
  • 1
  • 5
  • 8

16 Answers16

81
WebSettings settings = wbView.getSettings();
settings.setDomStorageEnabled(true);
Black
  • 18,150
  • 39
  • 158
  • 271
zionpi
  • 2,593
  • 27
  • 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).

Black
  • 18,150
  • 39
  • 158
  • 271
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
  • 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 '17 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 '19 at 09: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..

Black
  • 18,150
  • 39
  • 158
  • 271
ridoy
  • 6,274
  • 2
  • 29
  • 60
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","");

AbdulHannan
  • 358
  • 1
  • 15
  • This actually resolved a blank issue when replacing this overload: .LoadData(html, mime, encoding). – CRice Apr 20 '22 at 06: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.

Chris Neve
  • 2,164
  • 2
  • 20
  • 33
  • 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. – Jeff Camera Nov 09 '21 at 19:26
  • Thank you very much, it was my problem of untrusted certif – ΩlostA Mar 29 '22 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.

Shez Ratnani
  • 312
  • 3
  • 15
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!

Chris White
  • 709
  • 1
  • 5
  • 8
4

clearView is deprecated.

Use:

webView.loadUrl("about:blank")
Yura Shinkarev
  • 5,134
  • 7
  • 34
  • 57
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
}
gite_rahul
  • 21
  • 3
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)
araxis
  • 36
  • 1
  • 7
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;
    }
}
Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Anchal Radhwani
  • 156
  • 1
  • 15
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.
Onic Team
  • 1,620
  • 5
  • 26
  • 37
Nelson
  • 43
  • 5
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>
live-love
  • 48,840
  • 22
  • 240
  • 204
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.