960
votes

I am changing the value of an EditText on keyListener.

But when I change the text the cursor is moving to the beginning of the EditText. I need the cursor to be at the end of the text.

How to move the cursor to the end of the text in a EditText.

25
I came across the same issue. But what I asked myself better is why this is happening, so that I could resolve the issue before instead of having to move the caret myself.kaneda
@kaneda I totally agree, however it would have been helpful if you had added the actual solution.AgentKnopf
@Zainodis It was just a thought. Like I said I came across the same issue, which does not necessarily means that I've found the solution. In my case I had the issue with the EditTexts as items of a ListView. As for experimentation, I made some changes on the ListView source code itself, which is a rather complex beast, and tested on the emulator. It was related to focus control management made by the component. So of course it's not a solution I could give to help our friend. :)kaneda
This does not work inside OnFocusChanged callback. The solution there is to put setSelection inside a runnable and run it on main thread. Check here stackoverflow.com/a/32156989/4514796Ali Nem

25 Answers

1397
votes

Try this:

UPDATE:

Kotlin:

editText.setSelection(editText.text.toString().length)//placing cursor at the end of the text

Java:

editText.setSelection(editText.getText().length());
239
votes

There is a function called append for ediitext which appends the string value to current edittext value and places the cursor at the end of the value. You can have the string value as the current ediitext value itself and call append();

myedittext.append("current_this_edittext_string"); 
154
votes

Kotlin:

set the cursor to the starting position:

val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(0)

set the cursor to the end of the EditText:

val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(editText.text.length)

Below Code is to place the cursor after the second character:

val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(2)

JAVA:

set the cursor to the starting position:

 EditText editText = (EditText)findViewById(R.id.edittext_id);
 editText.setSelection(0);

set the cursor to the end of the EditText:

EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(editText.getText().length());

Below Code is to place the cursor after the second character:

EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(2);
129
votes

If you called setText before and the new text didn't get layout phase call setSelection in a separate runnable fired by View.post(Runnable) (repost from this topic).

So, for me this code works:

editText.setText("text");
editText.post(new Runnable() {
         @Override
         public void run() {
             editText.setSelection(editText.getText().length());
         }
});

Edit 05/16/2019: Right now I'm using Kotlin extension for that:

fun EditText.placeCursorToEnd() {
    this.setSelection(this.text.length)
}

and then - editText.placeCursorToEnd().

43
votes

You could also place the cursor at the end of the text in the EditText view like this:

EditText et = (EditText)findViewById(R.id.textview);
int textLength = et.getText().length();
et.setSelection(textLength, textLength);
29
votes
editText.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        editText.setSelection(editText.getText().length());
        return false;
    }
});
22
votes

This is another possible solution:

et.append("");

Just try this solution if it doesn't work for any reason:

et.setSelection(et.getText().length());
16
votes

In my case I created the following kotlin ext. function, may be useful to someone

 private fun EditText.focus(){
        requestFocus()
        setSelection(length())
    }

Then use as follows

mEditText.focus()
14
votes

If your EditText is not clear:

editText.setText("");
editText.append("New text");

or

editText.setText(null);
editText.append("New text");
14
votes

You should be able to achieve that with the help of EditText's method setSelection(), see here

13
votes
/**
 * Set cursor to end of text in edittext when user clicks Next on Keyboard.
 */
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            ((EditText) view).setSelection(((EditText) view).getText().length());
        }
    }
};

mEditFirstName.setOnFocusChangeListener(onFocusChangeListener); 
mEditLastName.setOnFocusChangeListener(onFocusChangeListener);

It work good for me!

12
votes

i think this can achieve what you want.

 Editable etext = mSubjectTextEditor.getText();
 Selection.setSelection(etext, etext.length());
10
votes

The question is old and answered, but I think it may be useful to have this answer if you want to use the newly released DataBinding tools for Android, just set this in your XML :

<data>
    <variable name="stringValue" type="String"/>
</data>
...
<EditText
        ...
        android:text="@={stringValue}"
        android:selection="@{stringValue.length()}"
        ...
/>
7
votes

editText.setSelection is the magic here. Basically selection gives you place cursor at any position you want.

EditText editText = findViewById(R.id.editText);
editText.setSelection(editText.getText().length());

This places cursor to end of EditText. Basically editText.getText().length() gives you text length. Then you use setSelection with length.

editText.setSelection(0);

It is for setting cursor at start position (0).

7
votes

enter image description here

Hello Try This

 <EditText
       android:id="@+id/edt_text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Hello World!"
       android:cursorVisible="true"/>

EditText editText = findViewById(R.id.editText); editText.setSelection(editText.getText().length()); // End pointCursor

6
votes

If you want to select all text and just entering new text instead of old one, you can use

    android:selectAllOnFocus="true"
6
votes

This works

Editable etext = edittext.getText();
Selection.setSelection(etext,edittext.getText().toString().length());
4
votes

All the other codes I've tested didn't work good due to the fact that the user could still place the caret/cursor wherever in the middle of the string (ex.: 12|3.00 - where | is the cursor). My solution always puts the cursor in the end of the string whenever the touch occurs on the EditText.

The ultimate solution is:

// For a EditText like:
<EditText
                android:id="@+id/EditTextAmount"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:hint="@string/amount"
                android:layout_weight="1"
                android:text="@string/zero_value"
                android:inputType="text|numberDecimal"
                android:maxLength="13"/>

@string/amount="0.00" @string/zero_value="0.00"

// Create a Static boolean flag
private static boolean returnNext; 


// Set caret/cursor to the end on focus change
EditTextAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View editText, boolean hasFocus) {
                if(hasFocus){
                    ((EditText) editText).setSelection(((EditText) editText).getText().length());
                }
            }
        });

// Create a touch listener and put caret to the end (no matter where the user touched in the middle of the string)
EditTextAmount.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View editText, MotionEvent event) {
                ((EditText) editText).onTouchEvent(event);
                ((EditText) editText).setSelection(((EditText) editText).getText().length());
                return true;
            }
        });


// Implement a Currency Mask with addTextChangedListener
EditTextAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String input = s.toString();
                String output = new String();
                String buffer = new String();
                String decimals = new String();
                String numbers = Integer.toString(Integer.parseInt(input.replaceAll("[^0-9]", "")));

                if(returnNext){
                    returnNext = false;
                    return;
                }

                returnNext = true;

                if (numbers.equals("0")){
                    output += "0.00";
                }
                else if (numbers.length() <= 2){
                    output += "0." + String.format("%02d", Integer.parseInt(numbers));
                }
                else if(numbers.length() >= 3){
                    decimals = numbers.substring(numbers.length() - 2);
                    int commaCounter = 0;
                    for(int i=numbers.length()-3; i>=0; i--){
                        if(commaCounter == 3){
                            buffer += ",";
                            commaCounter = 0;
                        }
                        buffer += numbers.charAt(i);
                        commaCounter++;
                    }
                    output = new StringBuilder(buffer).reverse().toString() + "." + decimals;
                }
                EditTextAmount.setText(output);
                EditTextAmount.setSelection(EditTextAmount.getText().length());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                /*String input = s.toString();
                if(input.equals("0.0")){
                    EditTextAmount.setText("0.00");
                    EditTextAmount.setSelection(EditTextAmount.getText().length());
                    return;
                }*/
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Hope it helps!

4
votes

For ViewModel, LiveData and Data binding

I needed this functionality for EditText with multiline support in my notes app. I wanted the cursor at the end of the text when the user navigates to the fragment that has note text.

The solution suggested by the djleop comes close. But the problem with this is that, if the user puts the cursor somewhere in the middle of the text for editing and starts typing, the cursor would jump to the end of text again. This happened because the LiveData would emit the new value and cursor would jump to the end of the text again resulting in user not able to edit the text somewhere in the middle.

To solve this, I use MediatorLiveData and assign it the length of String only once using a flag. This will cause the LiveData to read the value only once, that is, when the user navigates to the fragment. After that the user can place the cursor anywhere they want to edit the text there.

ViewModel

private var accessedPosition: Boolean = false

val cursorPosition = MediatorLiveData<Event<Int>>().apply {
    addSource(yourObject) { value ->
        if(!accessedPosition) {
            setValue(Event(yourObject.note.length))
            accessedPosition = true
        }
    }
}

Here, yourObject is another LiveData retrieved from the database that holds the String text that you are displaying in the EditText.

Then bind this MediatorLiveData to your EditText using binding adapter.

XML

Uses two-way data binding for displaying text as well as accepting the text input.

<!-- android:text must be placed before cursorPosition otherwise we'll get IndexOutOfBounds exception-->
<EditText
    android:text="@={viewModel.noteText}"
    cursorPosition="@{viewModel.cursorPosition}" />

Binding Adapter

@BindingAdapter("cursorPosition")
fun bindCursorPosition(editText: EditText, event: Event<Int>?) {
    event?.getContentIfNotHandled()?.let { editText.setSelection(it) }
}

Event class

The Event class here is like a SingleLiveEvent written by Jose Alcérreca from Google. I use it here to take care of screen rotation. Using the single Event will make sure that the cursor won't jump to the end of text when the user is editing the text somewhere in the middle and the screen rotates. It will maintain the same position when the screen rotates.

Here's the Event class:

open class Event<out T>(private val content: T) {

    var hasBeenHandled = false
        private set // Allow external read but not write

    /**
     * Returns the content and prevents its use again.
     */
    fun getContentIfNotHandled(): T? {
        return if (hasBeenHandled) {
            null
        } else {
            hasBeenHandled = true
            content
        }
    }

    /**
     * Returns the content, even if it's already been handled.
     */
    fun peekContent(): T = content
}

This is the solution that works for me and provides good user experience. Hope it helps in your projects too.

3
votes

similar to @Anh Duy's answer, but it didnt work for me. i also needed the cursor to move to the end only when the user taps the edit text and still be able to select the position of the cursor afterwards, this is the only code that has worked for me

boolean textFocus = false; //define somewhere globally in the class

//in onFinishInflate() or somewhere
editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        editText.onTouchEvent(event);

        if(!textFocus) {
            editText.setSelection(editText.getText().length());
            textFocus = true;
        }

        return true;
    }
});

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        textFocus = false;
    }
});
3
votes

This does the trick safely:

    editText.setText("");
    if (!TextUtils.isEmpty(text)) {
        editText.append(text);
    }
3
votes
etSSID.setSelection(etSSID.getText().length());
3
votes

The above answers didnt worked for me. So i found out a new solution. This maybe helpful to someone. I have been using the latest version of Android Studio i.e 3.5 as per the present date. Maybe that might be the reason y the above answers didnt showed any effect.

CODE:

EditText available_seats = findViewById(R.id.available_seats);
Selection.setSelection(available_seats.getText(),available_seats.getText().length());

Here, the 1st argument is the Spannable text value that you want to playwith whereas the 2nd argument is the index value. Since, we want the cursor at the end of the text, we took getText().length() which returns the length of the text

2
votes
public class CustomEditText extends EditText {
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        this.setSelection(this.getText().length());
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {

    }
}

User this CustomEditText in your XML file, this will work. I have tested this and it is working for me.

2
votes

If you want to Place cursor at the end of text in EditText view

 EditText rename;
 String title = "title_goes_here";
 int counts = (int) title.length();
 rename.setSelection(counts);
 rename.setText(title);