0

I have an custom Edittext that is extended from AppCompatEditText and It shows date text like "10.02.2012 10:40" When user clicks one time over any part of texts, the part should be selected automatically.

For example:

enter image description here enter image description here

To do it, in my custom edittext, I overrode onSelectonChange

@Override
    protected void onSelectionChanged(int selStart, int selEnd) {

        if (isFocused() && isCursorVisible() && isPressed()) {
            int[] aFoo = findPartOfText(selStart, selEnd);
            selStart = aFoo [0];
            selEnd = aFoo [1];
        }

        super.onSelectionChanged(selStart, selEnd);

    }

It doesn't change anything. I have also tried this :

@Override
        protected void onSelectionChanged(int selStart, int selEnd) {
            //Select first 3 characters
            super.onSelectionChanged(0, 2);

        }

Result is same. It doesn't select the first 3 characters as well.

ziLk
  • 3,120
  • 21
  • 45
  • this can be achieved using an onClickListener or OnFocusChangedListener. here is the link [link](http://stackoverflow.com/questions/9128297/how-to-dynamically-select-text-from-edittext-onclicklistener) – kush gupt May 05 '17 at 12:16

1 Answers1

0

Try EditText.setSelection(int start, int end). when onSelectionChanged is called, check the cursor position or selection, and call setSelection to change it.

jarly
  • 231
  • 1
  • 4
  • 16
  • setSelection already calls onSelectionChanged method. So Calling setSelection method In onSelectionChanged doens't make sense. Note: I have also tried your way before. – ziLk May 05 '17 at 12:32
  • In onSelectionChanged, check the selection range. Only call setSelection if the range is not what you want. – jarly May 05 '17 at 12:34