How to set focus to right of text in EditText for android?
setSelection wasn't working for me, but this works like a charm. Works on afterTextChanged as well.
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
edittext.requestFocus(edittext.getText().length());
}
You can explicitly put caret to last position in text:
EditText editText = (EditText) findViewById(R.id.textId);
int pos = editText.getText().length();
editText.setSelection(pos);
Something more especific about that you ask, you can use the next code:
EditText editText = (EditText) findViewById(R.id.textId);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
editText.setSelection(editText.getText().length());
}
}
});
the method setOnFocusChangeLister() is used for detect when the editText receive the focus.