Android Nougat 7.1.1 showAtLocation(...) Gravity not working
Asked Answered
T

3

3

This is related to this question: Android Nougat PopupWindow showAsDropDown(...) Gravity not working

However, when I applied this fix:

if (android.os.Build.VERSION.SDK_INT >=24) {
    int[] a = new int[2];
    anchorView.getLocationInWindow(a);
    popUp.showAtLocation(((Activity) mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+anchorView.getHeight());
} else{
    popUp.showAsDropDown(anchorView);
}

It doesn't work on Android Nougat 7.1.1. Particularly on Google Pixel and Nexus 6p devices.

Has anybody got a fix for this? Please share. https://code.google.com/p/android/issues/detail?id=231487

Tankersley answered 1/2, 2017 at 7:10 Comment(1)
Have you found a workaround for this? If not, I've written a bug report about it here: code.google.com/p/android/issues/detail?id=233237Castello
P
6

When I change PopupWindow's height from WindowManager.LayoutParams.MATCH_PARENT to WindowManager.LayoutParams.WRAP_CONTENT, it works on Android 7.1, I don't know the reason, but maybe you can try it.

Also, you need to change your code to:

if (android.os.Build.VERSION.SDK_INT == 24) {
    int[] a = new int[2];
    anchorView.getLocationInWindow(a);
    popUp.showAtLocation(((Activity)mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+anchorView.getHeight());
} else{
    popUp.showAsDropDown(anchorView);
}
Polyclitus answered 7/2, 2017 at 8:12 Comment(1)
It is not work for we. My device is Nexus 5x Android 7.1.1Oligarch
P
1
public void showFilterWindow(Context context, PopupWindow popupWindow,View showView, int xoff, int yoff) {
    if (Build.VERSION.SDK_INT < 24) {
        //7.0 The following system is used normally
        popupWindow.showAsDropDown(showView, xoff, yoff);
    } else {
        int[] location = new int[2];
        showView.getLocationOnScreen(location);
        int offsetY = location[1] + showView.getHeight() + yoff;
        if (Build.VERSION.SDK_INT == 25) {
            //【note!】Gets the screen height without the virtual key
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int screenHeight = wm.getDefaultDisplay().getHeight();
            /*
             * PopupWindow height for match_parent,
             * will occupy the entire screen, it needs to do special treatment in Android 7.1
            */
            popupWindow.setHeight(screenHeight - offsetY);
        }
        //Use showAtLocation to display pop-up windows
        popupWindow.showAtLocation(showView, Gravity.NO_GRAVITY, 0, offsetY);
    }
}

Reference https://mcmap.net/q/752080/-android-nougat-popupwindow-showasdropdown-gravity-not-working fix some bug.

Parapet answered 22/9, 2017 at 2:28 Comment(0)
I
0

I fixed it, my nexus 5 7.1.1 alread has bug. sample code:

    View rootView = anchor.getRootView();
    Rect rect = new Rect();
    rootView.getWindowVisibleDisplayFrame(rect);

    int[] xy = new int[2];
    anchor.getLocationInWindow(xy);
    int anchorY = xy[1] + anchor.getHeight();

    int height = rect.bottom - anchorY;

    PopupWindow poupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, height);
    poupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, anchorY);    
Illjudged answered 14/4, 2017 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.