The android SDK has lots of nice goodies built in to make your applications look sexier. One such feature is the blurring of windows. This effect looks particularly nice if a background window is blurred while a dialog box is shown above which can really make it stand out. Below shows the application such an example; on the left is the default about box (for WordCube Pro) and on the right is with added blur and no dimming.
I am using the AlterDialog.Builder to create my dialog, however this method will work with all kinds of dialog providing you can access it via getWindow.
dialog = new AlertDialog.Builder(WordCube.this) .setTitle(WordCube.this.getResources().getString(R.string.app_name)) .setMessage(s) .setIcon(R.drawable.logo) .setPositiveButton(R.string.btn_close, null) .show();
Below shows the code needed to add blur and remove dimming of the background (as I think the blur looks nicer when the background is well lit).
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); lp.dimAmount=0.0f; dialog.getWindow().setAttributes(lp); dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
The blur is simply created using the last line (line 4) which sets a flag for the dialog telling android that we want windows below this one to be blurred. To achieve the dimming, we need to retrieve the layout parameters for the dialog window, set the dim amount to zero, update these parameters with setAttributes (lines 1-3).
Any comments, questions, or improvements please let me know.
One word Thank You. I was trying to do this – your site answered in 5 seconds. Thank you Thank you.
Is it possible that removing blur, dimming of the background using your technique ?
I just want a transparent background window of a dialog.
I haven’t tested it but I think this is how you would do it:
By setting alpha to 0.5 it should make the window 50% transparent. Try it out let me know how it goes…
Thanks! People don’t waste 3 hours like I did. You have to SHOW the dialog first or in my case with a custom dialog setContentView() first BEFORE, like so. Or it WONT work.
SkillMenuView skillMenuView = new SkillMenuView(this);
setContentView(skillMenuView);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.dimAmount = 0.0F;
lp.screenBrightness = 1.0F;
getWindow().setAttributes(lp);
This helped out immensely.
I tried to override the AlertDialog theme originally and was not having very much success.
It worked like a charmed!
Thank you so much!
setContentView(R.layout.main);
mDialog = new ProgressDialog(this); mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setCancelable(true);
mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
lp.dimAmount = 0.0f;
mDialog.getWindow().setAttributes(lp);
Doesn’t really work… I even tried to modify the styles.xml file. Please help.
Valuable information , worked perfectly for the alertDialog.
Thanks. You helped very much.
Thank you very very much for such precise and fully-working piece of code.
I also searched for that for minutes and your site just answered me in 4 lines and 5 secondes 🙂
Thx
I cannot get this to work. Any help is greatly appreciated. It states that “The method getWindow() is undefined for the type AlertDialog.Builder”. Here’s the code I’m using:
public static void showEula(final boolean accepted, final Activity activity) {
AlertDialog.Builder eula = new AlertDialog.Builder(activity)
.setTitle(R.string.eula_title)
.setIcon(android.R.drawable.ic_dialog_info)
.setMessage(activity.getString(R.raw.eula))
.setCancelable(accepted);
if (accepted) {
// If they’ve accepted the EULA allow, show an OK to dismiss.
eula.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
} else {
// If they haven’t accepted the EULA allow, show accept/decline buttons and exit on
// decline.
eula
.setPositiveButton(R.string.accept,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
setAcceptedEula(activity);
dialog.dismiss();
}
})
.setNegativeButton(R.string.decline,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
activity.finish();
}
});
}
eula.show();
WindowManager.LayoutParams lp = eula.getWindow().getAttributes();
lp.dimAmount = 0.0F;
eula.getWindow().setAttributes(lp);
eula.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
}
@BILL
You should create the alertdialog from the builder.
AlertDialog a = eula.create();
a.show();
and then call a.getwindows().getattribues() on the alert dialog.
Without showing the alert dialog get window will fail.
I want to do it on pop up of POPUPWINDOW.I tried but it is not working
To dim background you have to set the dim amount as well as the flag
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount=0.6f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
How can we do this for a POP-UP WINDOW?
Note that blur is deprecated and on Android ICS you will get black behind for dialog instead of blur.
Is it only works on dialog? i want to use it on inflated view, is there any way to make it?
This wouldn’t work with DialogFragment.
Very good man!!
[…] There is a really good tutorial here that shows you how to do this: https://www.stealthcopter.com/blog/2010/01/android-blurring-and-dimming-background-windows-from-dialo… […]
To: Chad
THANK YOU for posting your comment. I was just about to give up hope of controlling the dialog’s background.
Pete
how can i add it into “hello world” first app?
i’m newbie and
where to add:
dialog = new AlertDialog.Builder(WordCube.this)
.setTitle(WordCube.this.getResources().getString(R.string.app_name))
.setMessage(s)
.setIcon(R.drawable.logo)
.setPositiveButton(R.string.btn_close, null)
.show();
and where to add:
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount=0.0f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
and finally main.xml code look like?
can u help me? i’m newbie
thank u very much!
Thanks a lot. You saved me a lot of time. Thank you so much.
hi, thanks but this not worked in new android api and is deprecated.
what is best way now?
Thank you
The above example is for dialogbox i want the same for popup.
Please help me how to do this
Thank you
FLAG_BLUR_BEHIND deprecated and not working anymore!
very good
very nice
Its So Easy ………… Thank you so much!!!!.
thanks for great post .
[…] ここ(Android: Blurring and dimming background windows from dialogs)にそれっぽい回答がございました。 […]
[…] tomada de Android: el Desenfoque y la atenuación de fondo de las ventanas de diálogo. Estoy teniendo problemas para obtener el contenido en mi cuadro de diálogo desenfoque. Cuando se […]
To those still looking for blurring behind dialogs and bottom sheets, I’ve created a custom BlurBottomSheet which is direct child of DialogFragment (Gist here: https://gist.github.com/premacck/add41c3b43e7053a017b0a74a05014e0) which has blurred and dimmed background. You can customize it enough and get a dialog instead of BottomSheet!
[…] reprise de Android: le Flou et la gradation de fond des fenêtres de dialogue. Je vais avoir du mal à obtenir le contenu sous mon dialogue de flou. Lors de l'appel […]