etmsg.setText("Updated Text From another Activity");
int position = etmsg.length();
Editable etext = etmsg.getText();
Selection.setSelection(etext, position);
static final String LINE_SEPARATOR = System.getProperty("line.separator");
int getIndexFromPos(int line, int column) {
int lineCount = getTrueLineCount();
if (line < 0) line = getLayout().getLineForOffset(getSelectionStart()); // No line, take current line
if (line >= lineCount) line = lineCount - 1; // Line out of bounds, take last line
String content = getText().toString() + LINE_SEPARATOR;
int currentLine = 0;
for (int i = 0; i < content.length(); i++) {
if (currentLine == line) {
int lineLength = content.substring(i, content.length()).indexOf(LINE_SEPARATOR);
if (column < 0 || column > lineLength) return i + lineLength; // No column or column out of bounds, take last column
else return i + column;
}
if (String.valueOf(content.charAt(i)).equals(LINE_SEPARATOR)) currentLine++;
}
return -1; // Should not happen
}
// Fast alternative to StringUtils.countMatches(getText().toString(), LINE_SEPARATOR) + 1
public int getTrueLineCount() {
int count;
String text = getText().toString();
StringReader sr = new StringReader(text);
LineNumberReader lnr = new LineNumberReader(sr);
try {
lnr.skip(Long.MAX_VALUE);
count = lnr.getLineNumber() + 1;
} catch (IOException e) {
count = 0; // Should not happen
}
sr.close();
return count;
}