269

I have an EditText with some dummy text in it. When the user clicks on it I want it to be selected so that when the user starts typing the dummy text gets deleted.

How can I achieve this?

Galip
  • 5,435
  • 10
  • 37
  • 47

12 Answers12

584

You can try in your main.xml file:

android:selectAllOnFocus="true"

Or, in Java, use

editText.setSelectAllOnFocus(true);
Shlok Jhawar
  • 277
  • 2
  • 3
  • 19
TheCottonSilk
  • 8,662
  • 2
  • 26
  • 37
165
editText.setSelectAllOnFocus(true);

This works if you want to do it programatically.

j0k
  • 22,600
  • 28
  • 79
  • 90
creftos
  • 2,090
  • 1
  • 18
  • 22
  • 2
    Also for me.. the above solution somehow did not work.. the programatically worked – Martin Christmann Aug 30 '13 at 13:27
  • 3
    For those who still having problems with the accepted answer above: try this one instead. Confirmed to be working (also tested for `Fragments`). – Hadi Satrio Jul 31 '15 at 10:10
  • By some weird reason xml way didn't work appropriately and this one do. Looks like bug inside of SDK. – ar-g Aug 30 '16 at 10:45
  • And if you are programmatically focusing (`editText.requestFocus()` issue `editText.clearFocus()` beforehand to ensure desired action occurs in the case where the view is _already_ focused – Bad Loser Sep 13 '18 at 22:03
36
EditText dummy = ... 

// android.view.View.OnFocusChangeListener
dummy.setOnFocusChangeListener(new OnFocusChangeListener(){
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus) && (isDummyText())
            ((EditText)v).selectAll();
    }
});
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
xandy
  • 27,357
  • 8
  • 59
  • 64
  • Thanks for your help, but I prefer TheCottonSilk's solution because I have 12 EditText's and I like to keep my code as clean as possible :) Thanks anyways! – Galip Jan 12 '11 at 13:56
  • @TheCottonSilk's solution is best for design time widgets and Craftos answer is best for dyamic editTexts. – Bishnu Paudel Dec 29 '12 at 05:40
  • 2
    @Galip `OnFocusChangeListener listener = new OnFocusChangeListener`... etc... then `editText1.setOnFocusChangeListener(listener)` `editText2.setOnFocusChangeListener(listener)` and so on... – SparK Oct 07 '13 at 20:30
28

I know you've found a solution, but really the proper way to do what you're asking is to just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 5
    In my app those fields are filled up by personal details which can be saved. When I load those details it'll replace the 'dummy' text. With my curren method I can also have text selected AFTER the user entered details. Thanks for your concerns though! – Galip Jan 12 '11 at 15:30
16

Managed it by calling focus and selection on program

{
editabletext.requestFocus();
editabletext.selectAll();
}
AMD
  • 1,662
  • 18
  • 39
11

Why don't you try android:hint="hint" to provide the hint to the user..!!

The "hint" will automatically disappear when the user clicks on the edittextbox. its the proper and best solution.

Vaibhav Vajani
  • 396
  • 5
  • 12
  • 5
    Good answer. However there might be situations you populate the EditText with values you read from settings etc. and you want to make it as easy as possible to change these values :) – nuala Aug 02 '12 at 14:36
7

You can also add an OnClick Method to the editText after

_editText.setSelectAllOnFocus(true);

and in that:

_editText.clearFocus();
_editText.requestFocus();

As soon as you click the editText the whole text is selected.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
user4596287
  • 71
  • 1
  • 1
6

SelectAllOnFocus works the first time the EditText gets focus, but if you want to select the text every time the user clicks on it, you need to call editText.clearFocus() in between times.

For example, if your app has one EditText and one button, clicking the button after changing the EditText leaves the focus in the EditText. Then the user has to use the cursor handle and the backspace key to delete what's in the EditText before they can enter a new value. So call editText.clearFocus() in the Button's onClick method.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
3

I tried an above answer and didn't work until I switched the statements. This is what worked for me:

    myEditText.setSelectAllOnFocus(true);
    myEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onClickMyEditText();
        }
    });

   private void onClickMyEditText() {
       if(myEditText.isFocused()){
        myEditText.clearFocus();
        myEditText.requestFocus();
       }else{
           myEditText.requestFocus();
           myEditText.clearFocus();
       }
   }

I had to ask if the focus is on the EditText, and if not request the focus first and then clear it. Otherwise the next times I clicked on the EditText the virtual keyboard would never appear

RipperJugo
  • 85
  • 4
2

Just add this to your editText in the .xml file

android:selectAllOnFocus="true"

2

This works like a charm:

XML

android:selectAllOnFocus="true"

Java

editText.setSelectAllOnFocus(true);

Learn more at TextView - android:selectAllOnFocus

K M Rejowan Ahmmed
  • 1,034
  • 2
  • 12
  • 26
1

This is works for me:

myEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(myEditText.isFocused()){
                myEditText.requestFocus();
                myEditText.clearFocus();
                myEditText.setSelection(myEditText.getText().length(), 0);
            }else{
                myEditText.requestFocus();
                myEditText.clearFocus();
            }

        }
    });