Blurring/dimming background windows when dialog is displayed

Quickly share your Android Code Snippets here...

Blurring/dimming background windows when dialog is displayed

Postby stealthcopter » Tue Jan 19, 2010 7:26 pm

I just added this as a post on my blog and thought it might be useful here too:

I am using the AlterDialog.Builder to create my dialog (see code below), however this method will work with all kinds of dialog providing you can access it via getWindow.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.    dialog = new AlertDialog.Builder(WordCube.this)
  3.  
  4.    .setTitle(WordCube.this.getResources().getString(R.string.app_name))
  5.  
  6.    .setMessage(s)
  7.  
  8.    .setIcon(R.drawable.logo)
  9.  
  10.    .setPositiveButton(R.string.btn_close, null)
  11.  
  12.    .show();
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); // retrieves the windows attributes
  3.  
  4. lp.dimAmount=0.0f; // sets the dimming amount to zero
  5.  
  6. dialog.getWindow().setAttributes(lp); // sets the updated windows attributes
  7.  
  8. dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); // adds the flag to blur bg
  9.  
  10.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


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

See below for a screenshot of this in action:

Image
stealthcopter
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Jan 17, 2010 11:42 am

Top

Re: Blurring/dimming background windows when dialog is displ

Postby moblcade.com » Wed Jun 15, 2011 9:59 pm

I just want to add that you can also disable background dimming altogether by clearing the appropriate flag:
Code: Select all
view.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests