Android EditText - Focus on it and select all text programmatically

I figured it out. Here is what I did.

I have a button user clicks when he enters user name (the positive button on DialogFragment). On button click event, I do this:

editUserName.setSelectAllOnFocus(true);
editUserName.selectAll();

This will focus and select all text in the EditText.


For selection of all the text of Edittext, you can use

editText.setSelection(0,editText.getText().toString().length());

and if you want to set cursor position you can use

editText.setSelection(position);

Tags:

Android