user928615
user928615

Reputation: 67

Progressbar on Android webview

I'm quite new to developing Android apps. Basically I'm making a webview template to display different URLs where the websites a mobile optimized.

The app has two pages:
1 splash page.
1 webview page.

I have worked through several tutorials and this part is actually working, so the problem is...

I want to show a progress bar when the webview is loading the website which then disappears when the websites have been loaded.

I have made a lot of tries to get this to work, but without success.

The XML file:
Basically have the webview with the ID: and a progress bar with ID:progressbar ( Not sure if this should be in there )

The JAVA File:
Link to preeview of java file

Upvotes: 2

Views: 2321

Answers (2)

SERPRO
SERPRO

Reputation: 10067

You can do that with this by adding a new property to your Activity:

protected ProgressDialog dialog;

When you are going to open the web add this:

dialog = ProgressDialog.show(this, "LOADING", "Website is loading...", true);
YourWebView.loadUrl(url);

/* Prevent WebView from Opening the Browser */
YourWebView.setWebViewClient(new InsideWebViewClient());

And then have to extend the WebViewClient class and add to the onPageFinished method:

protected class InsideWebViewClient extends WebViewClient {
  @Override
  public void onPageFinished(WebView view, String url)
  {
    super.onPageFinished(view, url);
    if(dialog != null && dialog.isShowing())
        dialog.dismiss();
  }
}

Upvotes: 1

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

You forgot to add ProgressBar support.

Add the below statement before setContentView(R.layout.layout_file);

// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

Then after a call to setContentView(R.layout.layout_file); add this line to make progressbar visible

// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

Now inside onProgressChanged(WebView view, int progress) you have to add following lines :

//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

// Return the app name after finish loading
    if(progress == 100)
         MyActivity.setTitle(R.string.app_name);

You can refer full tutorial here : ProgressBar in WebView. I have referred the code from that website only.

Edit

Another example from official Android docs WebView

Upvotes: 4

Related Questions