Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to highlight EditText programmatically?

Tags:

android

requestFocus() moves the cursor to the edit box, but does not highlight it. I want to highlight it like as if it was touched.

How can I do this?

like image 728
alex2k8 Avatar asked Jan 21 '10 00:01

alex2k8


2 Answers

Try this:

_field.setSelection( int startIndex, int endIndex);

The first parameter startIndex is the point in the string where you want to begin highlighting and the endIndex parameter is the point where you want to stop highlighting.

If you want to select all of the text use this instead:

_field.selectAll();
like image 55
JeremyFromEarth Avatar answered Nov 06 '22 09:11

JeremyFromEarth


I found a bug. My code was some thing like this:

edit = new EditText();
edit.requestFocus()
container.addView(edit);

It moved cursor to new 'edit', but did not highlight it. This fixed it:

edit = new EditText();
container.addView(edit);
edit.requestFocus();
like image 33
alex2k8 Avatar answered Nov 06 '22 10:11

alex2k8