Search code examples
javaandroidandroid-edittext

How to insert a text in EditText?


How to insert a text in EditText at the cursor position? editText.setText erases the current text and sets the new one, which is not what I want. editText.setText(editText.getText() + "my text"); inserts the text at the end, that's also not the case.


Solution

  • Its very easy to do this :

    int start =editText.getSelectionStart(); //Get cursor position with this function
    String str = "my text";//String you want to insert
    editText.getText().insert(start, str); //This will get the text and insert the String str at the current position.
    

    Hope this Helps!