title: Add loading spinner to webview Android app
body: <p>I am trying to add a loading spinner to show while the webpage loads.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.wEgyptianpost;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.os.Handler;
  import android.support.v4.widget.SwipeRefreshLayout;
  import android.widget.TextView;
  
  import java.util.Random;
  
  import com.google.android.gms.ads.AdRequest;
  
  import com.google.android.gms.ads.AdView;
  
  import com.wEgyptianpost.R;
  
  
  public class MainActivity extends Activity {
      WebView view;
      SwipeRefreshLayout mySwipeRefreshLayout;
  
      private WebView mWebView;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          AdView adView = (AdView) findViewById(R.id.admob_id);
  
          AdRequest adRequest = new AdRequest.Builder()
                  .setRequestAgent("android_studio:ad_template").build();
  
          adView.loadAd(adRequest);
  
  
  
          mWebView = (WebView) findViewById(R.id.activity_main_webview);
  
          final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
          final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
          swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
          swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
              @Override
              public void onRefresh() {
                  mWebView.reload();
                  swipeRefreshLayout.setRefreshing(true);
                  (new Handler()).postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          mWebView.stopLoading();
                          swipeRefreshLayout.setRefreshing(false);
  
  
  
  
  
                      }
                  },10000);
              }
          });
          // Force links and redirects to open in the WebView instead of in a browser
          //mWebView.setWebViewClient(new WebViewClient());
  
          // Enable Javascript
          WebSettings webSettings = mWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
  
          mWebView.getSettings().setUseWideViewPort(true);
          mWebView.getSettings().setLoadWithOverviewMode(true);
          // Use remote resource
           mWebView.loadUrl("http:google.com");
  
          // Stop local links and redirects from opening in browser instead of WebView
           mWebView.setWebViewClient(new MyAppWebViewClient());
  
          // Use local resource
          //mWebView.loadUrl("file:android_asset/web/google.html");
  
  
      }
  
  
  
      // Prevent the back-button from closing the app
      @Override
      public void onBackPressed() {
          if(mWebView.canGoBack()) {
              mWebView.goBack();
          } else {
              super.onBackPressed();
          }
      }
  
  
      @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 true;
      }
  
      @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>Here is my activity xml</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      tools:context=".MainActivity"&gt;
  
  
      &lt;android.support.v4.widget.SwipeRefreshLayout
      android:id="@+id/swipelayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity"&gt;
          &lt;WebView
              android:id="@+id/activity_main_webview"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" /&gt;
  
  
          &lt;ScrollView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"&gt;
  
     &lt;/ScrollView&gt;
  
  
  &lt;/android.support.v4.widget.SwipeRefreshLayout &gt;
  
      &lt;com.google.android.gms.ads.AdView android:id="@+id/admob_id"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          ads:adSize="BANNER"
          ads:adUnitId="ca-app-pub-1851250777225639/8300259410"
          android:layout_gravity="center_horizontal|bottom" /&gt;
  
      &lt;ProgressBar
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/progressBar2"
          android:layout_gravity="center" /&gt;
  
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>I added progress bar to the activity xml but I dont know how to link it to the main activity code.</p>