11

I use the following code to set the cursor at the end of customEditText(EditText) but it brings no effect.

customEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                 if (hasFocus) {
                    String customEditTextText = customEditText.getText().toString();
                    int selection = customEditTextText.length();
                    customEditText.setSelection(selection);
                }
            }
});

Has anybody any ideas for that ?

Jacob
  • 13,773
  • 18
  • 48
  • 69

7 Answers7

10

I had the same issue with an EditText inside an AlertDialog. The solution for me was this:

    mEditText.post(new Runnable() {
        @Override
        public void run() {
            mEditText.setSelection(mEditText.length());
        }
    });
ezefire
  • 702
  • 5
  • 18
7

Works for me:

editText.requestFocus();
editText.setSelection(editText.getText().length());
researcher
  • 1,591
  • 20
  • 22
5

This works for me

mEditText.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mEditText.setSelection(mEditText.getText().length());
                    }
                }, 50);

Don't know why, but need to use postDelayed to get it work. just post doesn't working

Codey
  • 402
  • 1
  • 8
  • 22
  • none of the above worked for me . i had to use setSelection method on my edit text in motion event runnable i was using to make the softKeyinput keyboard slide up when fragment opens up – Antroid Apr 18 '19 at 13:58
1

try this

customEditText.setSelection(customEditText.getText().length());

No need to convert it to String while Setting selection...

Lal
  • 14,505
  • 4
  • 39
  • 63
1

Try this....

editText.requestFocus();

Note : It automatically set cursor to last position of edittext.

1

In order to setSelection to be work softkeyboard should be openned..

if(binding.edtListName.requestFocus()) {
            DataManager.showKeyboard(binding.edtListName,context);

        }

DataManager class:

public static void showKeyboard(EditText editText, Context context){
        editText.requestFocus();
        editText.postDelayed(new Runnable(){
                                 @Override public void run(){
                                     InputMethodManager keyboard=(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
                                     keyboard.showSoftInput(editText,0);

                                     if (editText.getText().toString().length() > 0 ) {

                                         editText.setSelection(0,editText.getText().toString().length());
                                     }
                                 }
                             }
                ,400);
    }
Ucdemir
  • 2,070
  • 2
  • 21
  • 35
0

Try this

customEditText.setSelection(customEditText.getText().length()-1);
essess
  • 277
  • 3
  • 7
  • I believe it will not give him correct solution, but since its customedittext, there is some other issue like layout. so a trial approach. the code what is posted is same what you commented. – essess May 18 '14 at 07:33
  • Sorry, not same as my code, but same what is asked in question. Also i had similar issue when using rounded customized edit text, but strangely cursor was not visible, but issue was something else. Since i cannot comment on questions, i have to write it as an answer. – essess May 18 '14 at 07:38
  • Okk.. @essess let him try both the answers..I just asked you..Sorry if you felt bad.. – Lal May 18 '14 at 07:39
  • He wants to point to the end of the text. Since 0 is left of the beginning of the text, text.length points to the right of the text which is what the goal is (as far as I understand) – Felipe Centeno May 22 '19 at 16:48