6

I have an EditText and its text gets selected from through code. But I want to allow users to cut/copy the selected text. However, the cut/copy context menu doesn't appear until user long presses on text. But then it loses the actual selection. So, I'm thinking to show the context menu as the text get selected by the code.

I tried this inside onFocusChanged, but nothing appeared.

openContextMenu(EditText);

enter image description here

xmen
  • 1,947
  • 2
  • 25
  • 47

1 Answers1

4

IF I follow you usecase correctly, you can open context menu from the onFocusChangeListener registered on the testedEditText.

I prepared some small test for that which seems to be correctly supporting your usecase. You need to openMenu on the hooks which are selecting the content in the EditText.

public class Main extends Activity {

private EditText testedEditText;
private Button selectingButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    selectingButton = (Button) findViewById(R.id.button);
    testedEditText = (EditText) findViewById(R.id.textView);
    registerForContextMenu(testedEditText);

    selectingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            testedEditText.setSelection(6, 11);
            openContextMenu(testedEditText);
        }
    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.cmenu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.select_all:

            return true;
        case R.id.copy:
            //do something
            return true;
        case R.id.cut:
            //do something
            return true;

        case R.id.paste:
            //do something
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

}

Weirdly enough registering testedEditText.requestFocus(), and setting onFocusChangedListener for EditText was not enough.

Additional xml files for reference: cmenu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/select_all"
      android:title="select all"
      />
<item android:id="@+id/copy"
      android:title="copy"
      />
<item android:id="@+id/cut"
      android:title="cut"
      />
<item android:id="@+id/paste"
      android:title="paste"
      />
</menu>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<EditText android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World, Initial Text..."
        android:layout_centerInParent="true"
        />

<Button android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="button"
        />
</RelativeLayout>
Patrick
  • 472
  • 3
  • 16
  • There is no resource 'android.R.menu.cmenu'. Eclipse is showing error. I'm afraid it may go bad in future releases. Resource name may change. +1 for good try. – xmen Jun 21 '14 at 08:03
  • I added missing resources: 1. menu put in /res/menu 2. second one is just a layout. – Patrick Jun 21 '14 at 08:17
  • 2
    I do not think it will show the menu(quick actions) same as shown in screenshot but a normal context menu created using ListView. – xmen Jun 21 '14 at 09:23
  • yea, that is some custom view, was thinking the problem was only hooking it to some events. So the thing is that you need the view too? – Patrick Jun 21 '14 at 09:25
  • I only need to show quick actions bar, the device's default one. I do not think it can be called directly from API. – xmen Jun 21 '14 at 09:31
  • @xmen I am also trying to show native edittext context menu. Could you help in answering the question? Here is the question link : https://stackoverflow.com/questions/47195345/how-to-show-copy-paste-options-context-menu-for-multiautocompletetextview-on-b – Kaustubh Nov 09 '17 at 09:52
  • @xmen, did you find the solution for this? – Manas Aug 19 '20 at 03:07