Chris White
Chris White

Reputation: 739

android webview displaying blank page

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?

Upvotes: 44

Views: 82904

Answers (16)

Ivan Ivanov
Ivan Ivanov

Reputation: 1

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.

Upvotes: 0

Shez Ratnani
Shez Ratnani

Reputation: 312

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

Upvotes: 5

araxis
araxis

Reputation: 36

just use loadDataWithBaseURL

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

Upvotes: 1

Chris Neve
Chris Neve

Reputation: 2434

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.

Upvotes: 7

ridoy
ridoy

Reputation: 6342

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

Upvotes: 12

Veaceslav Gaidarji
Veaceslav Gaidarji

Reputation: 4311

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

Upvotes: 14

zionpi
zionpi

Reputation: 2671

WebSettings settings = wbView.getSettings();
settings.setDomStorageEnabled(true);

Upvotes: 82

gite_rahul
gite_rahul

Reputation: 21

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
}

Upvotes: 2

Anchal Radhwani
Anchal Radhwani

Reputation: 148

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

Upvotes: 0

live-love
live-love

Reputation: 52524

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>

Upvotes: 0

Nelson
Nelson

Reputation: 43

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.

Upvotes: 0

Mehmet Sefa Balık
Mehmet Sefa Balık

Reputation: 499

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

Upvotes: 3

user4115463
user4115463

Reputation:

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

Upvotes: 0

Yura Shinkarev
Yura Shinkarev

Reputation: 5364

clearView is deprecated.

Use:

webView.loadUrl("about:blank")

Upvotes: 5

Chris White
Chris White

Reputation: 739

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!

Upvotes: 4

AbdulHannan
AbdulHannan

Reputation: 368

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","");

Upvotes: 11

Related Questions