heisenberg3008
heisenberg3008

Reputation: 65

Dim or blur background of popup window

I am able to get the pop up window but the background is transparent also I am able to read the contents of the activity behind it. I want to dim the background so the pop up and the older activity gets differentiated.

Upvotes: 2

Views: 2292

Answers (2)

cesards
cesards

Reputation: 16319

From API 31+, there are two window attributes we can use to achieve that.

<item name="android:windowBlurBehindEnabled">true</item>
<item name="android:windowBlurBehindRadius">Xdp</item>

If your foal is to run an Activity Dialog-alike, then we have more flags we might want to explore:

<!-- Removes Window Title. windowActionBar=false is not needed. -->
<item name="windowNoTitle">true</item>

<!-- When enabling Blur, we also need to provide the blur radius. -->
<!-- Only available on Min Sdk 21. -->
<item name="android:windowBlurBehindEnabled">true</item>
<item name="android:windowBlurBehindRadius">12dp</item>

<!-- In case we want our Activity to behave like a Dialog. -->
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowIsFloating">true</item>

<!-- We can mix blur + dim or we can just go with blur. This is to remove the dimmed bg. -->
<item name="android:backgroundDimEnabled">false</item>

I haven't figured out what's the best option for retro-compatibility purposes, but as soon as I find it I'll update the post.

Upvotes: 0

sissyphus_69
sissyphus_69

Reputation: 350

You can do it in 2 ways
1. by adding background color with transparency, to the parent of your popup layout. example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cc000000">

<YOUR_POPUP_VIEW
.... /> 
</RelativeLayout>
  1. WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.dimAmount = #WHAT_EVER_VALUE_YOU_WANT_TO_KEEP; //Generally in between 0.70f to 0.80f getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getWindow().setAttributes(layoutParams);

Upvotes: 1

Related Questions