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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
s89aduasda
  • 231
  • 3
  • 7
  • 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. – alanv Mar 25 '15 at 20:08

3 Answers3

13

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

s89aduasda
  • 231
  • 3
  • 7
4

after set background,just add elevation.

popupWindow.setElevation(10);

this will work after API21.

wogsland
  • 9,106
  • 19
  • 57
  • 93
fabelYu
  • 41
  • 1
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));
KrzyShzy
  • 156
  • 7