Reputation: 5465
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?
Upvotes: 274
Views: 149176
Reputation: 1267
This works like a charm:
XML
android:selectAllOnFocus="true"
Java
editText.setSelectAllOnFocus(true);
Learn more at TextView - android:selectAllOnFocus
Upvotes: 4
Reputation: 8822
You can try in your main.xml
file:
android:selectAllOnFocus="true"
Or, in Java, use
editText.setSelectAllOnFocus(true);
Upvotes: 593
Reputation: 476
Just add this to your editText in the .xml file
android:selectAllOnFocus="true"
Upvotes: 2
Reputation: 51
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();
}
}
});
Upvotes: 1
Reputation: 85
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
Upvotes: 3
Reputation: 1722
Managed it by calling focus and selection on program
{
editabletext.requestFocus();
editabletext.selectAll();
}
Upvotes: 16
Reputation: 71
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.
Upvotes: 7
Reputation: 6462
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.
Upvotes: 6
Reputation: 27421
EditText dummy = ...
// android.view.View.OnFocusChangeListener
dummy.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) && (isDummyText())
((EditText)v).selectAll();
}
});
Upvotes: 35
Reputation: 2110
editText.setSelectAllOnFocus(true);
This works if you want to do it programatically.
Upvotes: 165
Reputation: 396
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.
Upvotes: 11
Reputation: 134714
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.
Upvotes: 28