Tuesday, April 10, 2012

Get cursor position of Edittext android or How to insert text at cursor position.


You can get the Cursor position using the getSelectionStart() and getSelectionEnd() methods. If no text is highlighted, both getSelectionStart() and getSelectionEnd() return the position of the cursor. myEditText.getSelectionStart();
or
myEditText.getSelectionEnd();

How to insert text at cursor position.
int curPos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, curPos);
String str2 = (String) str.substring(curPos);
editText.setText(str1+"<br/>"+ list1[which] +str2);

content -->