Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText.setSelection(end) brings no effect?

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 ?

like image 259
Jacob Avatar asked May 18 '14 07:05

Jacob


People also ask

How to implement edittext widget in Android?

The main layout of the application contains the EditText Widget and two buttons. To implement the UI invoke the following code inside the activity_main.xml file. To get an idea about how the basic EditText in android looks like. 1. Input Type for the EditText This is one of the attributes which is needed to be specified under the EditText widget.

What is the input type for the edittext?

1. Input Type for the EditText InputType Attribute Type of the Data which is entered number Mathematical numeric value phone Contact Number based on the country code date To take the date input time To take the time is neededinput 8 more rows ...

How to retrieve the edittext with the ID from an activity?

Provide the EditText with the id, by referring to the following code, which has to be invoked inside the activity_main.xml file. The following code needs to be invoked inside the MainActivity.kt file. Which performs the retrieving operation and provides the Toast message the same as the entered data.


2 Answers

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());
        }
    });
like image 92
ezefire Avatar answered Oct 19 '22 17:10

ezefire


Works for me:

editText.requestFocus();
editText.setSelection(editText.getText().length());
like image 9
researcher Avatar answered Oct 19 '22 17:10

researcher