Skip to content Skip to sidebar Skip to footer

SetSelection In EditText's OnFocusChange

Normally, when the view is clicked, the text cursor is set near the place you clicked on. I try to set it always to the end (past the last character), but it does nothing unless th

Solution 1:

No need delay. we need to wait setSelection or else automatically called done and execute my setSelection.

new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
            final EditText et = (EditText) v;
            et.post(new Runnable() {
                @Override
                public void run() {
                    et.setSelection(et.getText().length());
                }
            });
        }
    }
});

Solution 2:

You should call setSelection() inside the afterTextChanged() of the addTextChangedListener().


Post a Comment for "SetSelection In EditText's OnFocusChange"