Pinch Pinch - 22 days ago 9
Android Question

Show progress dialog on Android without blocking clicks on other UI elements

How do I show a progress dialog on Android without blocking clicks on the other UI elements on the screen (like Ads) ?

Answer

You have to use ProgressBar view, and create a separate overlay for the progress bar. The most transparent way would be to create an abstract Activity and override the setContentView() method (both with id, and View, shown only with id). Than you can inherit your Activities from this one, and use it as a normal Activity with the added benefit of showProgress and hideProgress methods.

private HashMap<View, ProgressBar> progressBars = new HashMap<View, ProgressBar>();
private RelativeLayout overlay;

public void setContentView(int id) {
   FrameLayout combinedView = new FrameLayout();
   combinedView.setLayoutParams(new ViewGroup.LayoutParams(
   LayoutParams.MATCH_PARENT,
   LayoutParams.MATCH_PARENT));
   View background = getLayoutInflater().inflate(id, null);
   background.setLayoutParams(new ViewGroup.LayoutParams(
   LayoutParams.MATCH_PARENT,
   LayoutParams.MATCH_PARENT));
   combinedView.addView(background);
   overlay = new RelativeLayout();
   overlay.setLayoutParams(new ViewGroup.LayoutParams(
   LayoutParams.MATCH_PARENT,
   LayoutParams.MATCH_PARENT));
   combinedView.addView(overlay);
   super.setContentView(combinedView);
}

public void showProgress(View view) {
   ProgressBar progressBar = progressBars.get(view);
   if (progressBar == null) {
      progressBar = new ProgressBar();
      progressBars.put(view, progressBar);
      progressBar.setLayoutParams(new ViewGroup.LayoutParams(
      LayoutParams.WRAP_CONTENT,
      LayoutParams.WRAP_CONTENT));
      overlay.addView(progressBar);
   }
   int[] position = view.getLocationOnScreen();
   int width = view.getWidth();
   int height = view.getHeight();
   int progressWidth = progressBar.getWidth();
   int progressHeight =progressBar.getHeight(); 
   progressBar.setMargins(position[0] + width / 2 - progressWidth/2,
   position[1] + height / 2 - progressHeight/2,0,0);
   progressBar.setVisibility(View.VISIBLE);
}

public void hideProgress(View view) {
   ProgressBar progressBar = progressBars.get(view);
   if (progressBar != null) {
      progressBar.setVisibility(View.INVISIBLE);
   }
}