4

I have Textfield and I want to copy content of it on my android device.

If I run it as a windows desktop application I can select the text right click I got the popup menu with possible actions.

Is there anyway to get this popup menu also on Android?

Harprit Kaur
  • 21
  • 1
  • 6
homik
  • 553
  • 5
  • 9

2 Answers2

5

The push and hold event can be easily simulated in JavaFX, as in this question.

Once you get the initial event, all you need to do is call the ContextMenu from the TextField. Since TextField.getContextMenu() won't return the default one, you can either provide your own or try to get the default one.

Getting the default one is a little bit more tricky, since it is part of the TextFieldBehavior class. It contains this method public void contextMenuRequested(ContextMenuEvent e);, so all you need to do is provide a ContextMenuEvent, and fire the event from the TextField.

This is a quick implementation:

public class BasicView extends View {

    public BasicView(String name) {
        super(name);

        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));
        });

        setCenter(new VBox(15.0, new Label("Push and hold for ContextMenu"), textField));
    }

    private void addPressAndHoldHandler(Node node, Duration holdTime, EventHandler<MouseEvent> handler) {
        class Wrapper<T> { 
            T content; 
        }

        Wrapper<MouseEvent> eventWrapper = new Wrapper<>();

        PauseTransition holdTimer = new PauseTransition(holdTime);
        holdTimer.setOnFinished(event -> handler.handle(eventWrapper.content));

        node.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
            eventWrapper.content = event;
            holdTimer.playFromStart();
        });
        node.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> holdTimer.stop());
        node.addEventHandler(MouseEvent.DRAG_DETECTED, event -> holdTimer.stop());
    }

    @Override
    protected void updateAppBar(AppBar appBar) {
        appBar.setTitleText("Push and Hold");
    }

}

On Desktop this is what you'll get:

The good news is you don't need to modify the ContextMenu for Android, JavaFX has a custom one:

Note the different menu items will change automatically based on the context, as in the Desktop popup.

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • What is the `TextFieldBehavior` - class? There are no actual class named that? And if you meant the TextField class you cannot mean `TextField.getContextMenu`... won´t return you the default one, and then say the defaut on is inside this `TextFieldBehaviour` class. Are these typo problems? The default context menu is inside the `ContextMenuEvent` isen´t it? – Lealo Sep 02 '17 at 13:55
  • No typos here. `TextFieldBehavior` is private (com.sun... package), so you can't access it. But if you fire the `ContextMenuEvent` it will be handled by `contextMenuRequested` in that class. – José Pereda Sep 02 '17 at 14:46
0

A quick search would show you something called "Context Menu". Here is small implementation :

TextView tv;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.tv1);
            registerForContextMenu(tv);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select The Action");
        menu.add(0, v.getId(), 0, "Copy");//groupId, itemId, order, title
    }
    @Override
    public boolean onContextItemSelected(MenuItem item){
        if(item.getTitle()=="Copy"){
            String text = tv.getText().toString();
            Log.e("onContextItemSelected",text);
        }

        return true;
    }

When you click on textView it shows a popup with given title and options from "onCreateContextMenu". Once you select an option "OnContextItemSelected" is called. Then you can use variable "text" as you wish. Don't forget to register the view for context menu using registerForContextMenu(tv).

nayakasu
  • 889
  • 1
  • 11
  • 32