6

so I tried android:selectAllOnFocus and of course I'm using android:hint. The app loads, requestFocus triggers and the full text is selected.

The problem is that when I click in the EditText the selection is lost.

I've already read: Select all text inside EditText when it gets focus

Community
  • 1
  • 1
Stephan Tual
  • 2,617
  • 3
  • 27
  • 49
  • 2
    That is perfectly normal. Otherwise, there is no way for the user to get rid of the selection. – CommonsWare Mar 24 '12 at 23:21
  • 1
    Have you tried selectAllOnFocus through java code in an onclick event? – AJcodez Mar 25 '12 at 00:00
  • 1
    what do you expect *should* happen? by your logic, the selection will never go away once the user has clicked it. is this really what you want? – Alex Lockwood Mar 25 '12 at 00:57
  • 3
    Thank you for the comments - the reason is the same as what you can see on the builtin android browser. Try going to a URL then click the URL bar, you'll find it automatically always selects the whole URL. The reason: having to delete it character by character would take too long. – Stephan Tual Mar 25 '12 at 10:38

6 Answers6

9

For some reason, it worked (on Jelly Bean) only if I posted it with a Handler:

new Handler().post(new Runnable() {
    @Override
    public void run() {
        paidView.selectAll();
    }
});
fhucho
  • 34,062
  • 40
  • 136
  • 186
1

Set the selection in an View.OnClickListener like so: (with the 0 and text length opposite to the previously approved response) - this will ensure that when the user starts typing the content will be overridden.

editText.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        editText.setSelection(editText.getText().length() - 1, 0);
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
1

I realize this is an old post, but I had this same problem. For me, the reasoning was that I like to move the keyboard out of the way (dismiss it) to get my bearings (or hit buttons to add or remove rows of data) and when i touched back on the EditText I was previously editing, it was annoying to have the keyboard pop back up and the text un-select, forcing me to either work to get the cursor to where I wanted to start deleting, or touch another EditText and then touch back on the original to re-select everything. I just want to have the keyboard pop back up and have the text selected and ready to overwrite whenever I go to that EditText.

There are easy solutions to this:

1) Long tap does this for you on most occasions.

2) If you're already using setSelectAllOnFocus(true), you can just throw a simple clearFocus() and requestFocus() in your onClick listener:

etMyEditText.setSelectAllOnFocus(true);
etMyEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        view.clearFocus();
        view.requestFocus();
    }
});

This way the EditText has everything selected when you tap on it, regardless of soft keyboard status.

Additional bonus: Add android:windowSoftInputMode="adjustPan" inside your <activity .../> tag in your AndroidManifest.xml file to force the selected EditText to stay in sight when the soft keyboard pops up.

Mike in SAT
  • 179
  • 2
  • 11
1

try This

editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    editText.selectAll();
    //or this.selectAll();
}

To select all the text.

Bosco
  • 1,536
  • 2
  • 13
  • 25
1

Set the selection in an View.OnClickListener like so:

editText.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        editText.setSelection(0, editText.getText().length() - 1);
    }
}
Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
0

Do the code below after you did findViewById and you are good to go:

    edtProductPrice.setSelectAllOnFocus(true);
    edtProductPrice.requestFocus();
    edtProductPrice.selectAll();
Arash Afsharpour
  • 1,282
  • 11
  • 22