Say in your xml's edittext section, add android:paddingLeft="100dp" This will move your start position of cursor 100dp right from left end. Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.
setSelection(editText. getText(). length()); This places cursor to end of EditText.
Where position is an int:
editText1.setSelection(position)
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);
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()
use the below line
e2.setSelection(e2.length());
e2
is edit text Object Name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With