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;