3

This is a simplified code for what I use to show a popup window when making a long click on a GridView item.

When the item on the most right the popup window gets adjusted to be within the screen.

But when the item near the bottom of the screen the popup window is cropped(part of it outside the screen).

How to get this fixed?

PopupWindow mDropDownMenu= new PopupWindow(list, WRAP_CONTENT, WRAP_CONTENT);

mDropDownMenu.showAsDropDown(aView);

aView is the GridView item.

The documentation says about showAsDropDown(View anchor)

 * Display the content view in a popup window anchored to the bottom-left
 * corner of the anchor view. If there is not enough room on screen to show
 * the popup in its entirety, this method tries to find a parent scroll
 * view to scroll. If no parent scroll view can be scrolled, the
 * bottom-left corner of the popup is pinned at the top left corner of the
 * anchor view.
 *

But it's always pinned to the bottom-left and don't go up-left.

Jack
  • 693
  • 1
  • 7
  • 25
  • Could you please provide a screenshot for better understanding. – Jatin Oct 11 '18 at 09:40
  • I have the same problem. There's an old post here https://stackoverflow.com/questions/13115401/android-popupwindow-showasdropdown-not-working-properly, But no working solution is given. – Bohsen Oct 11 '18 at 11:27
  • I've also tried using PopupWindowCompat on both API 22 and 25. No difference. The problem is still there. – Bohsen Oct 11 '18 at 11:35
  • If you set the height and width explicitly it works: PopupWindow(popupView, 100, 100).apply { setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) isTouchable = true isFocusable = true showAsDropDown(fragment_person_details__description) } – Bohsen Oct 11 '18 at 11:54
  • Possible duplicate of [Android PopupWindow showAsDropDown() not working properly](https://stackoverflow.com/questions/13115401/android-popupwindow-showasdropdown-not-working-properly) – Bohsen Oct 12 '18 at 06:16

2 Answers2

4

It turns out that I MUST set the drop down menu height to avoid this problem

List<DropDownListItem> items;

dropDown.setHeight( toPixels( 30 * items.size() ) );
Jack
  • 693
  • 1
  • 7
  • 25
1

Found the solution. You have to measure the view and set the measurement on the PopupWindow.

...
private fun showPopupWindow() {
        val popupView = layoutInflater.inflate(R.layout.standard_popup_window, null)
        popupView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        PopupWindow(popupView, popupView.measuredWidth, popupView.measuredHeight).apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            isTouchable = true
            isFocusable = true
            overlapAnchor = true
            width = popupView.measuredWidth
            height = popupView.measuredHeight
            contentView = popupView
            showAsDropDown(fragment_person_details__description)
        }
...
Bohsen
  • 4,242
  • 4
  • 32
  • 58
  • Thank you Bohsen, but is this an Android code? almost all of the code is not defined, including R.layout.standard_popup_window. – Jack Oct 11 '18 at 21:46
  • Also setOverlapAnchor(boolean) requires API 23 and I'm targeting API 16+ – Jack Oct 11 '18 at 21:54
  • This is Android kotlin code using android-kotlin extensions. Just drop setOverlapAnchor, should n't matter. Should work witout it. – Bohsen Oct 12 '18 at 06:02
  • R.layout.standard_popup_window is just the layout that I want to inflate into my popup window – Bohsen Oct 12 '18 at 06:14
  • @Jack There's a PopupWindowCompat class for that. – Gavin Wright Dec 17 '20 at 04:52