Android: Insert text into EditText at current position

AndroidInsertAndroid Edittext

Android Problem Overview


I want to insert a constant string into an EditText by the press of a button. The string should be inserted at the current position in the EditText. If I use EditText.append the text gets inserted at the end of the EditText.

How can I do that? I couldn't find a suitable method.

Android Solutions


Solution 1 - Android

Cpt.Ohlund gave me the right hint. I solved it, now, partly with using EditText.getSelectionStart(), but I realized that you can also replace the selected text with the same expression and you don't need String.subString() for that.

int start = Math.max(myEditText.getSelectionStart(), 0);
int end = Math.max(myEditText.getSelectionEnd(), 0);
myEditText.getText().replace(Math.min(start, end), Math.max(start, end),
        textToInsert, 0, textToInsert.length());

This works for both, inserting a text at the current position and replacing whatever text is selected by the user. The Math.max() is necessary in the first and second line because, if there is no selection or cursor in the EditText, getSelectionStart() and getSelectionEnd() will both return -1. The Math.min() and Math.max() in the third line is necessary because the user could have selected the text backwards and thus start would have a higher value than end which is not allowed for Editable.replace().

Solution 2 - Android

This seems simpler:

yourEditText.getText().insert(yourEditText.getSelectionStart(), "fizzbuzz");

However, Manuel's answer might be better if you want to replace any selected text with the inserted text.

Solution 3 - Android

Try using EditText.getSelectionStart() to get the current position of the cursor. Then you can use String.subString to get the text before and after the cursor and insert your text in the middle.

Solution 4 - Android

I think this function will help for you :

public void insertConstantStr(String insertStr) {
    String oriContent = editText.getText().toString();
    int index = editText.getSelectionStart() >= 0 ? editText.getSelectionStart() : 0;
    StringBuilder sBuilder = new StringBuilder(oriContent);
    sBuilder.insert(index, insertStr);
    editText.setText(sBuilder.toString());
    editText.setSelection(index + insertStr.length());
}

Solution 5 - Android

Editable editable = new SpannableStringBuilder("Pass a string here");

yourEditText.text = editable;

Solution 6 - Android

For Kotlin simply do that:

editText.text.insert(editText.selectionStart, "Your Text Here")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManuelView Question on Stackoverflow
Solution 1 - AndroidManuelView Answer on Stackoverflow
Solution 2 - AndroidOleg VaskevichView Answer on Stackoverflow
Solution 3 - AndroidCpt.OhlundView Answer on Stackoverflow
Solution 4 - AndroidwangzhengyiView Answer on Stackoverflow
Solution 5 - AndroidMSilvaView Answer on Stackoverflow
Solution 6 - AndroidGhayasView Answer on Stackoverflow