0

I wrote a library with a custom view that was extended from EditText.

I want when user calls setText() cursor will stay at the same position

But by default EditText setText() moves cursor at the beggining

enter image description here -> setText("1111") -> enter image description here

How can I fix it? Can I somehow override setText method (or other methods as bringPointToView() and so on)

EDIT 1: It is not a duplicate of this question

Because I want to override a standard behaviour of cursor at the Edittext.

And not force user of lib to call append() or setSelection()

EDIT 2: My Solution

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if (getText() != null) {
        Selection.setSelection(getText(), getText().length());
    }
}

1 Answers1

0

You should set text to "" and then append will move the cursor.

  YourEditText.setText("");
  YourEditText.append("youText");
Asad Ali
  • 319
  • 3
  • 14
  • This will only work if you're always editing the end of the EditText, otherwise editing text in the middle of the span will kick the cursor to the end on each entry. – robotsquidward Mar 11 '21 at 14:57