Tuesday 13 December 2011

Android - ProgressDialog without Dimming Screen

By default, when Android's ProgressDialog is shown, the rest of the screen is dimmed automatically. This is pretty useful in most cases, but sometimes you want to keep the content in the background clear and visible too.

This is possible albeit not overly obvious. It turns out that when ProgressDialog is constructed, it has a flag set specifying that behind the dialog should be dimmed. As passing in 0 to the constructor of the dialog as the chosen style doesn't work, you actually have to create the object, and then remove the offending flag manually.

An example follows, where this is an Activity:



ProgressDialog pd = new ProgressDialog(this);
pd.setIndeterminate(true);
pd.setTitle("Loading");
pd.setMessage("Please wait...");


// and now the magic
pd.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);


// not needed for this example, but allows the progress dialog to appear near the bottom, controlled by the float value.
pd.getWindow().setGravity(Gravity.BOTTOM);
pd.getWindow().getAttributes().verticalMargin = 0.1f;

7 comments:

  1. Very useful example.Thanks
    here is another useful example that lets you use ProgressDialog using threads,handlers and async tasks also . http://blog.creatiosoft.com/?p=1316

    ReplyDelete
  2. Replies
    1. Your comment isn't really as helpful as it could be, is it? It works for me, and seems to work for other people.

      So if isn't working for you, and you are genuinely looking for help making it work, perhaps identifying some key features of your environment might help work out why it isn't working for you. Maybe this trick only works for certain versions of Android. What version of Android? Are you trying on a real device (if so, which?) or the emulator? What "doesn't work"? Does it compile, does it crash, does it show the progress dialog but not dim?

      "Doesn't work" is a pretty frustratingly short response to offer as feedback when someone has went out of their way to post something they think will help others. Since you are a fellow developer, I'm sure you will appreciate this.

      Delete
  3. Thank you! It works great...very useful example. I tested it on a real device (Samsung Tab 10.1).

    ReplyDelete
  4. Both these modifications were exactly what I needed. Thanks a lot!! Cheers

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete