How to add a loading image to webview?
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 t开发者_开发百科hink that something wrong with async calls.
Thanks in advance ^^
regards
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?
精彩评论