anthony
anthony

Reputation: 7733

Select all text when EditText has focus doesn"t work

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!

Upvotes: 0

Views: 998

Answers (1)

Gazouu
Gazouu

Reputation: 382

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

Upvotes: 3

Related Questions