Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blur or dim background when Android PopupWindow active

I would like to be able to either blur or dim the background when I show my popup window using popup.showAtLocation, and unblur/dim the background when popup.dismiss is called.

I have tried applying layout params FLAG_BLUR_BEHIND and FLAG_DIM_BEHIND to my activity, but this appears to just blur and dim the background as soon my app is started.

How can I do blurring/dimming just with popups?

like image 530
k_day Avatar asked Jul 11 '10 01:07

k_day


People also ask

How to blur background when popup comes in android studio?

You need to fetch the dialog attributes first, then set up some alpha values for the dialog attributes. Now, your dialog with blur background is ready. But the important factor is to set a Flag FLAG_DIM_BEHIND for the window. Now the result is yours.

How can I dim the background when Bottomsheet is displayed without using dialog?

Using the Interface - onSlide which as a parameter slideOffSet of type float , can be used to dim the background. The new offset of this bottom sheet within [-1,1] range. Offset increases as this bottom sheet is moving upward.


1 Answers

The question was about the Popupwindow class, yet everybody has given answers that use the Dialog class. Thats pretty much useless if you need to use the Popupwindow class, because Popupwindow doesn't have a getWindow() method.

I've found a solution that actually works with Popupwindow. It only requires that the root of the xml file you use for the background activity is a FrameLayout. You can give the Framelayout element an android:foreground tag. What this tag does is specify a drawable resource that will be layered on top of the entire activity (that is, if the Framelayout is the root element in the xml file). You can then control the opacity (setAlpha()) of the foreground drawable.

You can use any drawable resource you like, but if you just want a dimming effect, create an xml file in the drawable folder with the <shape> tag as root.

<?xml version="1.0" encoding="utf-8"?> <shape     xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" >     <solid android:color="#000000" /> </shape> 

(See http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape for more info on the shape element). Note that I didn't specify an alpha value in the color tag that would make the drawable item transparent (e.g #ff000000). The reason for this is that any hardcoded alpha value seems to override any new alpha values we set via the setAlpha() in our code, so we don't want that. However, that means that the drawable item will initially be opaque (solid, non-transparent). So we need to make it transparent in the activity's onCreate() method.

Here's the Framelayout xml element code:

<FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/mainmenu"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:foreground="@drawable/shape_window_dim" > ... ... your activity's content ... </FrameLayout> 

Here's the Activity's onCreate() method:

public void onCreate( Bundle savedInstanceState) {   super.onCreate( savedInstanceState);    setContentView( R.layout.activity_mainmenu);    //   // Your own Activity initialization code   //    layout_MainMenu = (FrameLayout) findViewById( R.id.mainmenu);   layout_MainMenu.getForeground().setAlpha( 0); } 

Finally, the code to dim the activity:

layout_MainMenu.getForeground().setAlpha( 220); // dim  layout_MainMenu.getForeground().setAlpha( 0); // restore 

The alpha values go from 0 (opaque) to 255 (invisible). You should un-dim the activity when you dismiss the Popupwindow.

I haven't included code for showing and dismissing the Popupwindow, but here's a link to how it can be done: http://www.mobilemancer.com/2011/01/08/popup-window-in-android/

like image 148
uhmdown Avatar answered Oct 26 '22 12:10

uhmdown