Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PopupWindow elevation does not show shadow

Android PopupWindow does not show shadows when the elevation is set. It appears to support it from the documentation. I am using 5.0 Lollipop.

Creating the popup as follows:

    popupWindow = new PopupWindow(context);     popupWindow.setOutsideTouchable(true);     popupWindow.setFocusable(true);     popupWindow.setElevation(10);     popupWindow.setContentView(rootView);     popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, xPos, yPos); 
like image 268
Patrick Avatar asked Dec 02 '14 21:12

Patrick


People also ask

How do I set elevation in Android?

To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.

What is elevation in XML?

Elevation (Android) Elevation is the relative depth, or distance, between two surfaces along the z-axis. Specifications: Elevation is measured in the same units as the x and y axes, typically in density-independent pixels (dp).


2 Answers

As answered by an Android developer.

If the inflated view doesn't have a background set, or the popup window itself doesn't have a background set (or has a transparent background) then you won't get a shadow.

which was my case and seems to be yours, since you are not using setBackgroundDrawable.

This worked for me

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

I've opened a new issue suggesting that they update the documentation (https://code.google.com/p/android/issues/detail?id=174919)

like image 165
Maragues Avatar answered Sep 23 '22 15:09

Maragues


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); } 

PopupWindow with shadow

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.

like image 24
Suragch Avatar answered Sep 22 '22 15:09

Suragch