Search code examples
androidpopupandroid-popupwindow

How to adjust popup menu when pop outside container


I am using a custom popup menu that pops inside a RecycleView container.

When the view touched is the on at the bottom, the popup menu hides the controls bellow the RecycleView.

I want to adjust the popup position in this case, bringing the popup up the exact distance needed so the menu keeps inside the container.

This should be easy, but i am having difficulties getting the coordinates and applying the calculations from inside the Adapter that deals with clicks on items.

Here is what i managed so far:

void show(FragmentActivity activity, View touchedView, DataItem item) {

        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);

        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setOutsideTouchable(true);

        bind(popupWindow, popupView, item);

        //draws the menu perfectly bellow the touched element,but doesn't take in account the parent view area
        popupWindow.showAsDropDown(touchView);

    }

Solution

  • Found out. 2 Things were on the way and are explained in comments.

    void show(FragmentActivity activity, View touchView, IDataItem dataItem) {
    
            LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
    
            PopupWindow popupWindow = new PopupWindow(
                    popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
    
            popupWindow.setOutsideTouchable(true);
    
            bind(popupWindow, popupView, dataItem);
    
            //1º problem: View item is inside ViewHolder.Item, so container is 2 levels up and touched item one level up.
            View container = (View) touchView.getParent().getParent();
            View item = (View) touchView.getParent();
    
            int containerHeight = container.getHeight();
    
            //2º problem: to get size before the popup view is displayed:  ref:https://stackoverflow.com/a/15862282/2700303
            popupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    
            int yOff=0;
    
            //if popup view will draw pass the bottom, get the amount needed to adjust the y coordinate
            if (ContainerHeight < item.getHeight() + item.getTop() + popupView.getMeasuredHeight())
                yOff = ContainerHeight- (item.getTop() + item.getHeight() + popupView.getMeasuredHeight());
    
            popupWindow.showAsDropDown(touchView,0,yOff);
    
        }