There are two EditText
,while loading the page a text is set in the first EditText, So now cursor will be in the starting place of EditText
, I want
This code will help you to show your cursor at the last position of editing text.
editText.requestFocus();
editText.setSelection(editText.length());
I want to set cursor position in edittext which contains no data
There is only one position in an empty EditText, it's setSelection(0).
Or did you mean you want to get focus to your EditText
when your activity opens? In that case its requestFocus()
setSelection(int index)
method in Edittext
should allow you to do this.
I'm so late to answer this problem, so I figure it out. Just use,
android:gravity="center_horizontal"
I have done this way to set cursor position to end of the text after updating the text of EditText programmatically
here, etmsg
is EditText
etmsg.setText("Updated Text From another Activity");
int position = etmsg.length();
Editable etext = etmsg.getText();
Selection.setSelection(etext, position);
Below Code is Set cursor to Starting in EditText
:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(0);
Below Code is Set cursor to end of the EditText
:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(editText.getText().length());
Below Code is Set cursor after some 2th Character position :
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(2);