title: Show progress bar in MapFragment
body: <p>I'm using a MapFragment to display some POIs on a map. I don't understand how can I show, and where i have to define, a progress bar to inform the user of the POI downloading process. I already have the code of a progress view, defined in XML as a LinearLayout containing a ProgressBar and a TextView.</p>
  
  <p>Here is the FragmentMap code. I'm using both SherlockActionBar and Android Support v4.</p>
  
  <pre><code>public class MapFragment extends SherlockMapFragment {
  
  private static final String TAG = MapFragment.class.getSimpleName(); 
  private GoogleMap mMap;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      try {
           MapsInitializer.initialize(this.getActivity());
       } catch (GooglePlayServicesNotAvailableException e) {
           e.printStackTrace();
       }
      View root = super.onCreateView(inflater, container, savedInstanceState);
      int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getBaseContext());
      // Showing status
      if(status==ConnectionResult.SUCCESS) {
           mMap = getMap();
      }
      else {
          int requestCode = 10;
          Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
          dialog.show();
      }
  
      mMap.setMyLocationEnabled(true);
  
      LocationManager locationManager = (LocationManager) getActivity().getSystemService(Activity.LOCATION_SERVICE);
      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      String provider = locationManager.getBestProvider(criteria, true);
      Location myLocation= locationManager.getLastKnownLocation(provider);
      /*
      if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
          Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
              startActivity(gpsOptionsIntent);
      }
       */
      if(myLocation!=null)
          mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()), 15));
  
      new FetchAllPoiAsyncTask().execute();
      return root;
  }
  
  private class FetchAllPoiAsyncTask extends AsyncTask&lt;Void, Void, PoiCategory[]&gt; {
      @Override
      protected PoiCategory[] doInBackground(Void... params) {
          // TODO Auto-generated method stub
          HttpHeaders requestHeaders= getApplicationContext().createAuthenticatedHttpHeader();
  
          List&lt;MediaType&gt; acceptableMediaTypes = new ArrayList&lt;MediaType&gt;();
          acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
          requestHeaders.setAccept(acceptableMediaTypes);
          // Populate the headers in an HttpEntity object to use for the request
          HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate(true);
          // Perform the HTTP GET request
          try {
              ResponseEntity&lt;PoiCategory[]&gt; poiAll = restTemplate.exchange(getActivity().getString(R.string.poiAll_url), HttpMethod.GET, requestEntity, PoiCategory[].class, "provider1234");
              return poiAll.getBody();
          }
          catch(Exception e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
              return null;
          }
      }
  
      @Override
      protected void onPostExecute(final PoiCategory[] poiCategories) {
          if(poiCategories==null)
              Toast.makeText(getApplicationContext(), "An error occurred retrieving POIs", Toast.LENGTH_LONG).show();
          Log.d(TAG, "Retrieved "+poiCategories.length+" categories;");
          for(PoiCategory category : poiCategories) {
              Log.d(TAG, "Category "+category.getLabel()+" has "+category.getPoiList().length+" POIs;");
              Poi[] poiList = category.getPoiList();
              for(Poi poi : poiList) {
                  mMap.addMarker(new MarkerOptions()
                                      .position(parsePoiPosition(poi))
                                      .title(poi.getTitle())
                                      .snippet(poi.getDescription()));
              }
          }
      }
  }
  
  private MainApplication getApplicationContext(){
      return (MainApplication) getActivity().getApplicationContext();
  }
  
  public static LatLng parsePoiPosition(Poi poi) {
      double lat = Double.parseDouble(poi.getPositionLat());
      double lng = Double.parseDouble(poi.getPositionLong());
      LatLng result = new LatLng(lat, lng);
      return result;
  }
  }
  </code></pre>
  
  <p>I'm using this MapFragment in a DrawerLayout, adding it dynamically through fragment transactions. How can I add the progress view, setting it visible and invisible while the asyncTask downlaod the POIs? I have to add it to the FragmentMap or to the container DrawerLayout?</p>
  
  <p>EDIT</p>
  
  <p>I tried to modify the MapFragment this way:</p>
  
  <ol>
  <li>Define a layout in progress_bar.xml (shown later)</li>
  <li>Inflating it in the containerView through inflater.inflate()</li>
  <li>Setting visible/invisible in onPreExecute()/onPostExecute()</li>
  </ol>
  
  <p>But it doesn't works. ProgressBar is not shown, and POIs appears after some time in the map (I've added a Thread.sleep(5000) to simulate a long download)</p>
  
  <p>Here is FragmentMap modified code. The showProgress() method is already tested and works well in another activity.</p>
  
  <p>public class MapFragment extends SherlockMapFragment {</p>
  
  <pre><code>private static final String TAG = MapFragment.class.getSimpleName(); 
  private GoogleMap mMap;
  private View mDownloadProgressView;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      try {
           MapsInitializer.initialize(this.getActivity());
       } catch (GooglePlayServicesNotAvailableException e) {
           e.printStackTrace();
       }
      View root = super.onCreateView(inflater, container, savedInstanceState);
  
      inflater.inflate(R.layout.progress_bar, container);
      mDownloadProgressView= (View) getActivity().findViewById(R.id.download_progress);
  
      int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getBaseContext());
      // Showing status
      if(status==ConnectionResult.SUCCESS) {
           mMap = getMap();
      }
      else {
          int requestCode = 10;
          Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
          dialog.show();
      }
  
      mMap.setMyLocationEnabled(true);
  
      LocationManager locationManager = (LocationManager) getActivity().getSystemService(Activity.LOCATION_SERVICE);
      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      String provider = locationManager.getBestProvider(criteria, true);
      Location myLocation= locationManager.getLastKnownLocation(provider);
      /*
      if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
          Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
              startActivity(gpsOptionsIntent);
      }
       */
      if(myLocation!=null)
          mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()), 15));
  
      new FetchAllPoiAsyncTask().execute();
      return root;
  }
  
  private class FetchAllPoiAsyncTask extends AsyncTask&lt;Void, Void, PoiCategory[]&gt; {
  
      @Override
      protected void onPreExecute(){
          showProgress(true);
      }
  
      @Override
      protected PoiCategory[] doInBackground(Void... params) {
          // TODO Auto-generated method stub
          HttpHeaders requestHeaders= getApplicationContext().createAuthenticatedHttpHeader();
  
          List&lt;MediaType&gt; acceptableMediaTypes = new ArrayList&lt;MediaType&gt;();
          acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
          requestHeaders.setAccept(acceptableMediaTypes);
          // Populate the headers in an HttpEntity object to use for the request
          HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
          try {
              Thread.sleep(5000);
          } catch (InterruptedException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate(true);
          // Perform the HTTP GET request
          try {
              ResponseEntity&lt;PoiCategory[]&gt; poiAll = restTemplate.exchange(getActivity().getString(R.string.poiAll_url), HttpMethod.GET, requestEntity, PoiCategory[].class, "provider1234");
              return poiAll.getBody();
          }
          catch(Exception e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
              return null;
          }
      }
  
      @Override
      protected void onPostExecute(final PoiCategory[] poiCategories) {
          showProgress(false);
          if(poiCategories==null)
              Toast.makeText(getApplicationContext(), "An error occurred retrieving POIs", Toast.LENGTH_LONG).show();
          Log.d(TAG, "Retrieved "+poiCategories.length+" categories;");
          for(PoiCategory category : poiCategories) {
              Log.d(TAG, "Category "+category.getLabel()+" has "+category.getPoiList().length+" POIs;");
              Poi[] poiList = category.getPoiList();
              for(Poi poi : poiList) {
                  mMap.addMarker(new MarkerOptions()
                                      .position(parsePoiPosition(poi))
                                      .title(poi.getTitle())
                                      .snippet(poi.getDescription()));
              }
          }
      }
  }
  
  private MainApplication getApplicationContext(){
      return (MainApplication) getActivity().getApplicationContext();
  }
  
  public static LatLng parsePoiPosition(Poi poi) {
      double lat = Double.parseDouble(poi.getPositionLat());
      double lng = Double.parseDouble(poi.getPositionLong());
      LatLng result = new LatLng(lat, lng);
      return result;
  }
  
  /**
   * Shows the progress UI
   */
  @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  private void showProgress(final boolean show) {
      // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
      // for very easy animations. If available, use these APIs to fade-in
      // the progress spinner.
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB_MR2) {
          int shortAnimTime = getResources().getInteger(
                  android.R.integer.config_shortAnimTime);
          mDownloadProgressView.setVisibility(View.VISIBLE);
          mDownloadProgressView.animate().setDuration(shortAnimTime)
                  .alpha(show ? 1 : 0)
                  .setListener(new AnimatorListenerAdapter() {
                      @Override
                      public void onAnimationEnd(Animator animation) {
                          mDownloadProgressView.setVisibility(show ? View.VISIBLE
                                  : View.GONE);
                      }
                  });
      } else {
          // The ViewPropertyAnimator APIs are not available, so simply show
          // and hide the relevant UI components.
          mDownloadProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is progressBar.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/download_progress"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center_horizontal"
      android:orientation="vertical"
      android:visibility="gone" &gt;
  
      &lt;ProgressBar
          style="?android:attr/progressBarStyleLarge"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp" /&gt;
  
      &lt;TextView
          android:id="@+id/download_poi_status_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="16dp"
          android:fontFamily="sans-serif-light"
          android:text="@string/downlaod_progress_message"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>