4

I'm trying to get Android to select all the text in an EditText field when it gets the focus. I'm using this attribute in the layout (on both fields):

android:selectAllOnFocus="true"

I'm not sure if this is relevant, but to get the cursor to the first editable field (there's also a disabled field before it), I'm using the following commands:

quizStartNum.setFocusable(true);
quizStartNum.requestFocus();

But, while the cursor does move to the desired field when the layout is first displayed, the text doesn't get highlighted; instead the cursor ends up to the left of the text, the default behavior. If I move to the second field by touching it, all the text is selected as desired. Then, if I move back to the first field, again by touching it, the text is also completely selected. I would like to have the behavior right from the start. Is there a way to do this?

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213
  • I just encountered similar problem. Moreover, I can't show soft keyboard for this EditText using `InputMethodManager#showSoftInput(View, int)` while `InputMethodManager#toggleSoftInputFromWindow(IBinder, int, int)` works fine. – Seblis Aug 27 '13 at 07:32

6 Answers6

4

If android:selectAllOnFocus="true" does not work, try calling setSelectAllOnFocus(true) on that particular EditText.

If that doesn't work either, this is another workaround from a previous SO post.

EditText dummy = ... 

dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus) && (isDummyText())
            ((EditText)v).selectAll();
    }
});
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
dLobatog
  • 1,751
  • 16
  • 17
  • 1
    No luck. android:selectAllOnFocus="true" does not work, setSelectAllOnFocus(true) doesn't work, and the above workaround doesn't work. Here's the code I used to get it to compile: quizStartNum.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean arg1) { // TODO Auto-generated method stub if (v.hasFocus()) { ((EditText)v).selectAll(); } } }); – Jack BeNimble Jun 18 '11 at 04:21
1

I had a similar issue and android:selectAllOnFocus="true" did not work for me. The reason was that i was programatically requesting focus to the EditText before it was displayed. So if you are doing this for a EditText in an AlertDialog make sure you show it before you request focus to the EditText.

SubqueryCrunch
  • 1,325
  • 11
  • 17
1

EditText should be focussed after it displayed.

Following workout worked for me,

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Set Input Methode
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

@Override
public void onStart() {
    super.onStart();

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            Dialog dialog = getDialog();
            if (dialog != null) {
            // Set Layout 
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // Set Cancelable False
            setCancelable(false);

            // Set Focus Here <------------------------
            uiET_myTextView.requestFocus();
            }            
        }
    });
}
Burak Senel
  • 71
  • 2
  • 7
0

I also noticed that with android:selectAllOnFocus="true" seemed not to work, I used the mouse to select EditText in the emulator, but if I used my finger and touched it, as if on the phone, it worked. If you don't have a touch screen on your computer you may have to install your app on a physical device to test it. Android Studio may not recognize the mouse in this case

Steve C
  • 9
  • 5
0

Try removing your focus commands. They aren't necessary, Android should focus on the first field automatically?

citizen conn
  • 15,300
  • 3
  • 58
  • 80
-1

I'll refer to this older post.

If you just want your EditText to show a hint (for "what goes in here"), you might want to use the Android's XML-attribute hint (link).

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111