2

as we know in TextView or EditText after selecting text we have ContextMenu, my question is: how can i select text without having ContextMenu or disable showing that?

this code work, but that cause of can't selecting text

public class MyEditText extends AppCompatEditText {
    private final Context mContext;
    private       int     mPreviousCursorPosition;

    public MyEditText(Context context) {
        super(context);
        this.mContext = context;
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        CharSequence text = getText();
        if (text != null) {
            if (selStart != selEnd) {
                setSelection(selStart, selEnd);
                return;
            }
        }
        mPreviousCursorPosition = selStart;
        super.onSelectionChanged(selStart, selEnd);
    }
}
DolDurma
  • 15,753
  • 51
  • 198
  • 377

1 Answers1

0

For API level 11 or above do this:

edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {
        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            //This will prevent opening the menu
            return false;
        }

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }
    });

Further readings:

Darush
  • 11,403
  • 9
  • 62
  • 60