Skip to content

Instantly share code, notes, and snippets.

@kakajika
Created July 10, 2019 22:21
Show Gist options
  • Save kakajika/5f0f857a415eb278633cd43127b223ae to your computer and use it in GitHub Desktop.
Save kakajika/5f0f857a415eb278633cd43127b223ae to your computer and use it in GitHub Desktop.
PopupWindow with dim background
val popup = PopupWindow(...)
val container = popup.contentView.takeIf { it.layoutParams is WindowManager.LayoutParams }
?: (popup.contentView.parent as? View)?.takeIf { it.layoutParams is WindowManager.LayoutParams }
?: (popup.contentView.parent.parent as? View)?.takeIf { it.layoutParams is WindowManager.LayoutParams }
?: throw IllegalStateException("NO WindowManager.LayoutParams!")
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val lp = container.layoutParams as WindowManager.LayoutParams
lp.flags = lp.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
lp.dimAmount = 0.3f
wm.updateViewLayout(container, lp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment