How to set cursor position in EditText?

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 to set cursor position in the second EditText which contains no data. How to do this?


Solution 1:

Where position is an int:

editText1.setSelection(position)

Solution 2:

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

Solution 3:

How to Set EditText Cursor position in Android

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

Solution 4:

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