0

When an EditText has a focus, I want to select all text inside by default, but it doesn't work:

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                  // Go to the end
            editText.setSelection(getEditTextView().getText().length());

                    // Select all the content
                    editText.selectAll();
                }
            }
        });

Thank you very much guys!

anthony
  • 7,653
  • 8
  • 49
  • 101

1 Answers1

3

There are 2 good way to select the text in an EditText :

Inside your main.xml :

android:selectAllOnFocus="true"

Or :

editText.setSelectAllOnFocus(true); 

(If you want to do it programatically)

SOURCE : Select all text inside EditText when it gets focus

Gazouu
  • 372
  • 3
  • 16