1

Im using Gluon to develop javafx applications to Android, Iphone (and to desktop). When I export a test application to my Android phone (Marshmallow 6.0) - I cannot hold down onto text to access the menu from where you can copy text (the context menu) (Which is an example of what you can do with a context menu - and is not a question of how to copy text on long hold specifically in Android).

This was possible on iphone 6 when testing it there.

How can I detected wether the device/operating system has a default context menu or not in java?

lelelo
  • 173
  • 2
  • 12
  • 1
    See this [question](https://stackoverflow.com/questions/40561927/textfield-copy-action-on-long-hold-copy-popup/40564213#40564213) about getting the device's `ContextMenu`, and also enabling the long press. – José Pereda Sep 15 '17 at 16:09
  • Possible duplicate of [Textfield copy action on long hold (copy popup)](https://stackoverflow.com/questions/40561927/textfield-copy-action-on-long-hold-copy-popup) – Stephen Sep 15 '17 at 16:15
  • Thank you José. Stephen just... read the title or something I don´t know – lelelo Sep 15 '17 at 16:19

1 Answers1

2

On Desktop there is a default ContextMenu that is created and installed in TextFieldBehavior (private API). If you don't set your own custom context menu, that will be the one used when a ContextMenuEvent is fired (with a right click event for instance).

On mobile, both Android and iOS have a ContextMenu as well.

On iOS, it uses a native TextField (UITextField). When the long press event happens, it triggers the default context menu (on my iPad I can see a small magnifying glass, and after that the context menu shows up).

On Android, the JavaFX TextField has a custom skin, but shares the same private TextFieldBehavior as the desktop version. The problem in this case is the missing right click event that would trigger the ContextMenuEvent event.

That's why you have to fire manually a ContextMenuEvent event, as it was described in this question.

Conclusion: so far, this is basically required only on Android:

TextField textField = new TextField();

addPressAndHoldHandler(textField, Duration.seconds(1), event -> {
        Bounds bounds = textField.localToScreen(textField.getBoundsInLocal());
        textField.fireEvent(new ContextMenuEvent(ContextMenuEvent.CONTEXT_MENU_REQUESTED, 
                0, 0, bounds.getMinX() + 10, bounds.getMaxY() + 10, false, null));
    });
José Pereda
  • 44,311
  • 7
  • 104
  • 132