How to select all text when clicking on EditText?

so I tried Android: selectAllOnFocus and of course I use android: hint. The application loads requestFocus triggers and selects the full text.

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

I already read: Select all text inside EditText when it becomes focus

+6
source share
6 answers

For some reason, it worked (on Bean jelly) only if I placed it with a handler:

new Handler().post(new Runnable() { @Override public void run() { paidView.selectAll(); } }); 
+8
source

Set the selection in View.OnClickListener as follows:

 editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.setSelection(0, editText.getText().length() - 1); } } 
+1
source

Set the selection in View.OnClickListener as follows: (with a length of 0 and the length of the text opposite to the previously approved answer) - this ensures that when the user starts to enter text, he will be overridden.

 editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.setSelection(editText.getText().length() - 1, 0); } } 
+1
source

I understand this is an old post, but I had the same problem. For me, the argument was that I like to reset the keyboard (turn it off) to understand (or press buttons to add or delete data lines), and when I touched the EditText that I previously edited, it was unpleasant when the keyboard pop back and the text is not selected, forcing me to either work to hover over where I wanted to start deleting, or touch another EditText and then touch the original to re-select everything, I just want the keyboard to pop back, and the text is selected and ready to dub syaky time I come to the EditText.

There are simple solutions:

1) A long crane does this for you in most cases.

2) If you are already using setSelectAllOnFocus(true) , you can simply skip the 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(); } }); 

Thus, EditText has everything that was selected when you click on it, regardless of the state of the soft keyboard.

Extra bonus: Add android:windowSoftInputMode="adjustPan" to your <activity .../> in your AndroidManifest.xml file to force the selected EditText to appear when the soft keyboard appears.

+1
source

try this one

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

To select all text.

+1
source

Do the code below after you findViewById and all findViewById :

  edtProductPrice.setSelectAllOnFocus(true); edtProductPrice.requestFocus(); edtProductPrice.selectAll(); 
0
source

Source: https://habr.com/ru/post/911584/


All Articles