Issue
Okay, since it usually takes about 2 seconds for my WebView to load the page, I decided to display a "Loading" screen for my users. I found a very nice library called Shimmer that displays an animated textview. When my webview begins loading the page, I call setContentView to the shimmer layout and when it's done I call setContentView to the webview again. The problem is that the WebView is blank when I call setContentView. Why is that?
This is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO show you progress image
super.onPageStarted(view, url, favicon);
Log.i("WEBVIEW", "Loading");
shimmer = new Shimmer();
setContentView(R.layout.shimmer);
ShimmerTextView shimmerTextView = (ShimmerTextView) findViewById(R.id.shimmer_tv);
shimmer.start(shimmerTextView);
}
@Override
public void onPageFinished(WebView view, String url)
{
// TODO hide your progress image
super.onPageFinished(view, url);
Log.i("WEBVIEW", "Loading Done");
shimmer.cancel();
setContentView(R.layout.webview);
}
});
webView.loadUrl(url);
Solution
I am pretty sure you are implementing the Shimmer
wrong. You should not call setContentView()
more than once.
You should do something like this:
1) Add a Fragment
that contains your WebView
and make it load the url.
2) Add a Fragment
that contains the Shimmer
View
and start Shimmer
after you inflate the View
of the Fragment
in onCreateView()
.
3) When the WebView
finishes loading, use an event bus like Otto
to let the Activity
that contains Shimmer
Fragment
know to remove it.
Answered By - Emmanuel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.