Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Android ContextMenu Implementation #107

Merged
merged 6 commits into from
Dec 7, 2023
Merged

Conversation

mpiannucci
Copy link
Owner

This PR switches to use the native Android ContextMenu

Changes:

  • ContextMenu will now show where the user clicks, consistent with android native functionality
  • Nested Items are now supported on Android, the same as iOS

Breaking:

  • This may break icon functionality on android because I don't believe android ContextMenu's can have icons. @VidocqH can you test or chime in here before i merge this?

@VidocqH
Copy link
Contributor

VidocqH commented Dec 7, 2023

Since ContextMenu inherits from Menu, it should be created by MenuBuilder, so we can always use reflection to let it displays icons.

Parent Menu:
image

Child Menu(as a poc, I only set one icon here):
image

    private void setMenuIconDisplay(Menu contextMenu, boolean display) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);
            m.invoke(contextMenu, display);
        } catch (Exception ignored) {}
    }

    // Submenu example
    private void createContextMenuSubMenu(Menu menu, ReadableMap action, ReadableArray childActions, int i) {
        String title = action.getString("title");
        Menu parentMenu = menu.addSubMenu(title);

        @Nullable Drawable systemIcon = getResourceWithName(getContext(), action.getString("systemIcon"));
        menu.getItem(i).setIcon(systemIcon);  // set icon to current item.

        for (int j = 0; j < childActions.size(); j++) {
            createContextMenuAction(parentMenu, childActions.getMap(j), j, i);
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        contextMenu.clear();
        setMenuIconDisplay(contextMenu, true);
        ...
    }

@mpiannucci
Copy link
Owner Author

Thanks! That worked perfectly!

@mpiannucci mpiannucci merged commit e14b4ea into main Dec 7, 2023
@mpiannucci mpiannucci deleted the android-contextmenu branch December 7, 2023 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants