For others who visit this answer and missed what the OP already had, you should set the elevation to create a shadow:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupWindow.setElevation(20);
}
Depending on what your content view is, you might also need to set the background drawable, although this is not always necessary. If needed you can do as @Maragues suggested:
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
To support pre-Lollipop devices you could use a 9-patch or image that includes the shadow within it.
Code
This is the code for the image above.
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupWindow.setElevation(20);
}
popupWindow.showAtLocation(anyView, Gravity.CENTER, 0, 0);
Note:
The elevation is in pixels when set in code, but usually in dp when set in xml. You should convert a dp value to pixels when setting it in code.