Home > Android > Android: Blurring and dimming background windows from dialogs

Android: Blurring and dimming background windows from dialogs

January 17th, 2010 mat Leave a comment Go to comments

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.

android blur dim, before, after

android bluring and dimming effect before and after

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.


Bookmark and Share
Categories: Android Tags: , , ,
  1. Ravi
    January 27th, 2010 at 03:24 | #1

    One word Thank You. I was trying to do this – your site answered in 5 seconds. Thank you Thank you.

  2. jack
    January 28th, 2010 at 06:32 | #2

    Is it possible that removing blur, dimming of the background using your technique ?
    I just want a transparent background window of a dialog.

  3. January 29th, 2010 at 01:26 | #3

    I haven’t tested it but I think this is how you would do it:

    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
    lp.alpha=0.5f;
    dialog.getWindow().setAttributes(lp);
    

    By setting alpha to 0.5 it should make the window 50% transparent. Try it out let me know how it goes…

  4. Chad
    April 6th, 2010 at 05:35 | #4

    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);

  1. No trackbacks yet.
// unused langs // // // //