Search code examples
androidkotlinandroid-edittextcursor

How to let the user move cursor but not edit the editText in android kotlin


I'm making a calculator. As we all know every calculator uses a keyboard which is developed by a developer itself. I'm using editText to output based on which button the user clicks in. That's not a problem. The problem is I want the user to move the cursor around, select some part of the text entered but not edit it using the android keyboard. I used the focusable and cursorenabled method but nothing helped me. I hope someone of you can help me. I'm using kotlin. Thanks.


Solution

  • You can set whether the keyboard shows for an EditText using the setShowSoftInputOnFocus(Boolean) like so:

    editText.showSoftInputOnFocus = false
    

    This way the android keyboard won't show and the user can still move the cursor around and select text. Documentation here

    Note: This method is only available for API 21 and above (more than 94% of active devices as of October 2020).