2

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.

  • 1
    Why not use dialog(https://developer.android.com/guide/topics/ui/dialogs)? You can achieve the same pop-up + dim background with your custom dialog. – sissyphus_69 Apr 19 '20 at 14:03
  • Isn't it possible in windows? Cz I am almost done with my project and will have to change multiple files if I change to Dialog @sissyphus_69 – heisenberg3008 Apr 19 '20 at 14:09

2 Answers2

1

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);
sissyphus_69
  • 350
  • 5
  • 18
0

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.

cesards
  • 15,882
  • 11
  • 70
  • 65