Show context menu copy / paste to text using Android code

I have it EditText, and its text is selected from the code. But I want to allow users to cut / copy the selected text. However, the cut / copy context menu does not appear until the user clicks on the text. But then he loses the actual selection. So, I'm going to show the context menu as the text is selected by code.

I tried this inside onFocusChanged but nothing came up.

openContextMenu(EditText);

enter image description here

+4
source share
1 answer

IF I am following you correctly, you can open the context menu from the onFocusChangeListener registered on the verified EditText.

, , , usecase. Menu , 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);
    }
}

}

testedEditText.requestFocus(), onFocusChangedListener EditText .

xml : 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>
+4

Source: https://habr.com/ru/post/1545452/


All Articles