Set cursor focus position in Edittext

Asked

Viewed 651 times

0

Is there any way when I call the virtual keyboard (SoftKeyboard) set the cursor position?

I have a EditText which is already filled in by default the number 10, when the keyboard starts I want the cursor to be right after the 0, but he’s coming before the 1.

1 answer

3


It is possible using EditText.setSelection + editText.getText().length()

Just run:

editText.setSelection(editText.getText().length());

You can arrive when the field is in focus and fire the setSelection at this time

editText.setOnFocusChangeListener(new OnFocusChangeListener()
{
    @Override
    public void onFocusChange(View view, boolean hasFocus)
    {
        if (hasFocus) {
            editText.setSelection(editText.getText().length());
        }
    }
});
  • Thank you Ezequiel, I had misunderstood.

  • 1

    Imagine, I’m sorry if I wrote in a way that made your understanding difficult :)

  • @Ezequielmessore nothing, was fault on my own ;)

  • 1

    Take my +1... Very good!

  • 1

    It worked perfectly.... Thanks for your help

  • If you could help me again in the same question I would appreciate..... It is as well as the position of the focus to see all the selected text?

  • @Marciovieira tests this https://answall.com/q/241487/3635

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.