1

I am pretty new to android. I went through some tutorials and articles to work in android development. Just started to develop the app. I am progressing with texts and buttons etc.

I am showing some texts in view. If I long press the text in app, it shows the options for cut, copy etc.,

I just want to add one more item to this called "Greet". How to do this.

In iOS I am able to do this using the following code..

 UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:@"Greet" action:@selector(highlight)];

[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:highlightMenuItem]];

Following is the output in iOS.

enter image description here

How to do this same thing in Android.

I looked in some tutorials but all of them focused on adding new menus to app. I am looking for add a item to cut/copy menu.

Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23

1 Answers1

1

Add custom setCustomSelectionActionModeCallback

This ActionMode.Callback will be used to create the ActionMode when text selection is initiated in this View.

Like in this example

// Register a callback when user selects text
        tv.setCustomSelectionActionModeCallback(new Callback() {

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch(item.getItemId()){
                   //Switch cases
                }
                return false;
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
              //  inflate the menu
                getMenuInflater().inflate(R.menu.share_menu, menu);
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                /**
                 * Use the following code if you want to remove the default icons (select all, cut or copy). 
                 */
                // Remove the "select all" option
                //menu.removeItem(android.R.id.selectAll);
                // Remove the "cut" option
                //menu.removeItem(android.R.id.cut);
                // Remove the "copy all" option
                //menu.removeItem(android.R.id.copy);
                return false;
            }

        });
    }

    /**
     * Returns the selected text
     * @return String selectedText
     */
    private String getSelectedText() {
        String selectedText = "";
        int min = 0;
        int max = tv.getText().length();
        if (tv.isFocused()) {
            final int textStartIndex = tv.getSelectionStart();
            final int textEndIndex = tv.getSelectionEnd();

            min = Math.max(0, Math.min(textStartIndex, textEndIndex));
            max = Math.max(0, Math.max(textStartIndex, textEndIndex));
            selectedText = tv.getText().subSequence(min, max).toString().trim();
        }
        return selectedText;
        // Perform your actions with this selectedText
    }

See the references that will help you accomplish your task

Ref 1, Ref 2, Ref 3

See them all, and after that you will get a lot more idea on customizing your menu as per needed.

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68