2

I'm working with a webview in my android app... everytime I open an url my page is blank and after a while the url content appears... how can I add a loading image in webview during this transition time?? I tried to call open twice, first time loading a local content and next time the real url I wanted to feed but it doesn't work, I think that something wrong with async calls.

Thanks in advance ^^

regards

Erenwoid
  • 983
  • 6
  • 13
  • 25
  • [Check this out: How to show loading image or progress bar on WebView](https://stackoverflow.com/questions/39776146/how-to-show-loading-image-or-progress-bar-on-webview) – Ivan Wibawa May 23 '20 at 07:48

1 Answers1

2

Here's how I'm doing it.

In my main layout I have spots for the WebView and an ImageView. The WebView starts off 'gone' and the loadingimage is 'visible'.

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView android:id="@+id/imageLoading1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:visibility="visible"
        android:src="@drawable/loadingimage"
        />
    <WebView android:id="@+id/webView1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:visibility="gone"
        />

</LinearLayout>

Then in the java I load the URL. Once the URL is done loading, I swap out the loading image for the WebView.

        WebView wv;
        wv = (WebView) findViewById(R.id.webView1);
        wv.setWebViewClient(new WebViewClient() {
            //this is to catch link clicks on your page, to prevent opening a new browser
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                //hide loading image
                findViewById(R.id.imageLoading1).setVisibility(View.GONE);
                //show webview
                findViewById(R.id.webView1).setVisibility(View.VISIBLE);
            }
        });         
        wv.loadUrl("http://www.yourdomain.com/blahblahblah.htm");

Please note: the onPageFinished method I use here works fine for my simple application, however, it might not work for your case. For deets about how to fully listen for a page to finish loading, see this thread --> How to listen for a WebView finishing loading a URL?

Community
  • 1
  • 1
davehale23
  • 4,374
  • 2
  • 27
  • 40