title: How to display a progress bar when WebView loads a URL, in Android ？
body: <p><strong>I am new in android apps development and learning it.</strong></p>
  
  <p>I have developed an application which loads a website link using a <code>WebView</code>. And it's working fine.</p>
  
  <p>But I am facing problems to display and hide progress bar when the <code>WebView</code> loads a page.</p>
  
  <p>I want to display a progress bar (Just spinner in center) when the WebView loads the main link and internal links of a website and hide progress bar when page is completely loaded.</p>
  
  <p>My Java code:</p>
  
  <pre><code>package in.matebook.myapplication;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          WebView myWebView = (WebView) findViewById(R.id.webView);
          myWebView.setWebViewClient(new MyWebViewClient());
          WebSettings webSettings = myWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          myWebView.loadUrl("http://www.labnol.org");
      }
  
  
  
      private class MyWebViewClient extends WebViewClient {
  
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
  
              if (Uri.parse(url).getHost().equals("www.labnol.org")) {
                  // This is my web site, so do not override; let my WebView load the page
                  //or we can add more allowed host in if condition
                  return false;
              }
              // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              startActivity(intent);
              return true;
          }
  
  
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return false;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>And my layout XML code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"&gt;
  
  
      &lt;WebView  xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/webView"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          /&gt;
  
      &lt;ProgressBar
          android:id="@+id/progressBar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="?android:attr/progressBarStyleLarge"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Right now, the progress bar is displayed, but it does not hide.</p>
  
  <p>When I create <strong>progressbar</strong> object and add below line in Java code then the progress bar is not displayed.</p>
  
  <pre><code>progressbar.setVisibility(View.GONE);
  </code></pre>
  
  <p>I have also tried <a href="https://stackoverflow.com/questions/9375222/android-how-can-i-show-progress-bar-while-loading-data-to-webview">an other answer</a> but the progress bar does not work when I click on internal links.</p>
  
  <ol>
  <li><p>I want to display progress bar when main page load and hide progress bar when the main page is completely loaded.</p></li>
  <li><p>I want to display the progress bar again when the user clicks on internal links of the website and same as above, hide the progress bar when the page is completely loaded.</p></li>
  </ol>
  
  <p>Please help, I am beginner in Android apps development.</p>