Ken Nichols
Ken Nichols

Reputation: 93

Show Webview when page completly done loading?

For simplicity sake let’s say I have 1 activity that has a few textViews a progressSpinner and a webView.

The webView visibility is set to “gone” all others are set to visible. When the page finishes loading and the 2 divs I want to remove are hidden, I want to hide all visible items and show the fully loaded webView.

However when I run it I see all the visible items and the webView is not showing at this point(which is good). After a few seconds the visible items are gone and a blank webView appears(shouldn’t happen), then the page loads, then the 2 divs are hidden.

I want to hide this from the user how can I set it to display the webView only after the page is completely done loading and the 2 divs are hidden?

public void onPageFinished(WebView view, String url)  {  
    webview.loadUrl("javascript:$('#MainMenu').hide()");
    webview.loadUrl("javascript:$('#EmployeeDashboard').hide()");
    android.os.SystemClock.sleep(2500); // My attempt to allow the divs to be hidden before it's displayed to the user
    findViewById(R.id.progressSpinner).setVisibility(View.GONE);
    findViewById(R.id.imageView1).setVisibility(View.GONE);
    findViewById(R.id.textView1).setVisibility(View.GONE);
    findViewById(R.id.textView2).setVisibility(View.GONE);
    findViewById(R.id.tvLoading).setVisibility(View.GONE);
    findViewById(R.id.webview).setVisibility(View.VISIBLE);
} 

In the xml I have:

<WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:visibility="gone" />

Upvotes: 0

Views: 8153

Answers (2)

Ken Nichols
Ken Nichols

Reputation: 93

Found out I was having trouble because of a redirect, this helped me: How to listen for a WebView finishing loading a URL?

Upvotes: 1

Dave
Dave

Reputation: 298

I assume you're creating your webview object then calling loadUrl(...)

After that, instead of setting the contentView to your webView, set it to your view/graphic/whatever you want to show while it's loading.

In your onPageFinished, call setContentView to your webView.

MyWebView myWebView = new MyWebView();
myWebView.loadUrl ("http://whatever.com/goes/here");
setContentView(R.loadingurlspinner);

then in your onPageFinished()

//stuff you have above
setContentView(myWebView);

Upvotes: 2

Related Questions