androidx.appcompat.view.menu.MenuPopupHelper Java Examples

The following examples show how to use androidx.appcompat.view.menu.MenuPopupHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommonUtils.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
public static void showPopupOffset(@NonNull PopupMenu menu, int xoff, int yoff) {
    try {
        Field field = menu.getClass().getDeclaredField("mPopup");
        field.setAccessible(true);

        MenuPopupHelper helper = (MenuPopupHelper) field.get(menu);
        if (helper == null) throw new IllegalStateException();

        Method method;
        try {
            method = helper.getClass().getDeclaredMethod("show", int.class, int.class);
            method.setAccessible(true);
        } catch (NoSuchMethodException ex) {
            try {
                method = helper.getClass().getDeclaredMethod("tryShow", int.class, int.class);
                method.setAccessible(true);
            } catch (NoSuchMethodException exx) {
                menu.show();
                return;
            }
        }

        method.invoke(helper, xoff, yoff);
    } catch (NoSuchFieldException | IllegalAccessException | InvocationTargetException ignored) {
        menu.show();
    }
}