Android PopupWindow: White background but keep shadow?
Asked Answered
B

3

9

How do I style my PopupWindow in Android with a white background but still keep the shadow? I am trying to create something like [this][1]:

Objective

By default, my PopupWindow has a dark background. So I set the popup window's contents to have a white background which gives me this:

White content background

Which has a shadow but still has the black "border" which really is just the uncovered parts of the popup window background.

So I try and set the popup window background to white with:

popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE))

which causes this:

which gives the correct background colour but also removes the shadow.

So is there any easy way to keep the shadow but set the background as white. Is there something else I should use instead of PopupWindow to create what I want? Or do I have to use a 9 patch or something?

Benoit answered 25/3, 2015 at 19:13 Comment(1)
Prior to API 21, yes you have to use a 9-patch. Starting in API 21, popup windows have a default elevation that provides a shadow.Haiduk
B
13

I just ended up using a 9 patch created with http://inloop.github.io/shadow4android/

Benoit answered 27/3, 2015 at 1:24 Comment(0)
H
4

after set background,just add elevation.

popupWindow.setElevation(10);

this will work after API21.

Harryharsh answered 15/2, 2017 at 3:25 Comment(2)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { popupWindow.setElevation(100); }Olein
But how to do it for less than API 19Centaury
T
3

If nothing works, you can use below code. This will place a shadow around Popupwindow with white background. I've tested it in my app.

popupWindow.setBackgroundDrawable(context.getDrawable(android.R.drawable.picture_frame));
Tocci answered 26/3, 2018 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.