Put cursor at specific position in EditText component in Android

I found a lot of the same questions on several Android Dev forums: “How to put cursor at specific position in EditText?”. As I understood, new to platform developers can’t find proper method. At the first blush this is strange situation, because there is no such method as putCursor or setCursor, but if you will read JavaDocs more detailed you will find simple method setSelection(int position);. This method need to use for this situation.
I am going to show code snippet how to use it:

EditText editText = (EditText) findViewById(R.id.textId);
editText.setText("EditText component");
editText.setSelection(3);

In this snippet I initialize EditText component, set some text and put cursor at 3-rd position.

As you see it is simple.

Share

1 comment to Put cursor at specific position in EditText component in Android

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.