0

Trying to get current cursor position in my editText in Android. Didn't find any solution. Simply can't get cursor position but can set position. Now trying to set cursor in onTextChanged function like below:

private var prevValue = ""

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        prevValue = s.toString()
    }

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if (count - before > 1) {
            val newPos= s!!.length- abs(start-prevValue.length) 
            // newPos is incorrect. How can I calculate current cursor position?
            editText.setSelection(min(max(0,newPos),formattedValue.length))
            //I want to set cursor on the position where user typed any symbol

        }
    }

Any advices or help?

Akram Baratov
  • 143
  • 1
  • 3
  • 13

0 Answers0