Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - PopupWindow above a specific view

I am developing an application for Android and I am using a popup window when the user clicks a specific menu bar object(consisting of small images lined up horizontally) on the bottom of the screen.

On the click I want the popup window to be anchored to the top-left corner of the view that was clicked and be shown on top.

The only methods that seem to be relevant are showAsDropDown(View anchor, int xoff, int yoff) and showAtLocation(View parent, int gravity, int x, int y). The problem with showAsDropDown is that it is anchored to the bottom-left corner of the view.

Is there another way to implement this?

like image 400
Matt Avatar asked May 29 '13 15:05

Matt


People also ask

How to show popup window above view Android?

To display the popup window above the view, you can use the showAsDropDown() function. v: View(Anchor)

What is pop up window in Android?

android.widget.PopupWindow. This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.


2 Answers

popupWindow.showAtLocation(...) actually shows the window absolutely positioned on the screen (not even the application). The anchor in that call is only used for its window token. The coordinates are offsets from the given gravity.

What you actually want to use is:

popupWindow.showAsDropDown(anchor, offsetX, offsetY, gravity); 

This call is only available in API 19+, so in earlier versions you need to use:

popupWindow.showAsDropdown(anchor, offsetX, offsetY); 

These calls show the popup window relative to the specified anchor view. Note that the default gravity (when calling without specified gravity) is Gravity.TOP|Gravity.START so if you are explicitly using Gravity.LEFT in various spots in your app you will have a bad time :)

like image 178
William Avatar answered Sep 21 '22 05:09

William


You just needed to move the popupWindow by the height of its anchor using the yoff parameter in the showAsDropDown(View anchor, int xoff, int yoff) syntax.

popupWindow.showAsDropDown(anchor, 0, -anchor.getHeight()+popupView.getHeight); 

Also, be aware that if the max height allowed to anchor does not allow for the transformation, the popup might not show up properly.

like image 38
theSlyest Avatar answered Sep 18 '22 05:09

theSlyest