0Day Forums
Place cursor at the end of text in EditText - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Kotlin (https://0day.red/Forum-Kotlin)
+--- Thread: Place cursor at the end of text in EditText (/Thread-Place-cursor-at-the-end-of-text-in-EditText)

Pages: 1 2 3


Place cursor at the end of text in EditText - collazo688 - 07-20-2023

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`.


RE: Place cursor at the end of text in EditText - collembolan413313 - 07-20-2023

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");




RE: Place cursor at the end of text in EditText - bistort766 - 07-20-2023

i think this can achieve what you want.


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




RE: Place cursor at the end of text in EditText - marsiellaozsrb - 07-20-2023

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);



RE: Place cursor at the end of text in EditText - prothallium525784 - 07-20-2023

/**
* 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!



RE: Place cursor at the end of text in EditText - darrel196 - 07-20-2023

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;
}
});


RE: Place cursor at the end of text in EditText - vibes455 - 07-20-2023

editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
editText.setSelection(editText.getText().length());
return false;
}
});


RE: Place cursor at the end of text in EditText - buckqp - 07-20-2023

This does the trick **safely**:

editText.setText("");
if (!TextUtils.isEmpty(text)) {
editText.append(text);
}


RE: Place cursor at the end of text in EditText - domella944 - 07-20-2023

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

android:selectAllOnFocus="true"



RE: Place cursor at the end of text in EditText - constriction601515 - 07-20-2023

This is another possible solution:

et.append("");

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

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