I'm searching a way to move the cursor the a specific line for example the 150
to move the cursor to specific position we use
edittext.setSelection(pos);
but what about move to another line ? we assume the line 150
I'm searching a way to move the cursor the a specific line for example the 150
to move the cursor to specific position we use
edittext.setSelection(pos);
but what about move to another line ? we assume the line 150
This is a Kotlin snippet:
val lineCount = editText.layout.lineCount
You can get the number of lines in your edittext by using the getLineCount() method. This returns the total lines present, as a result of layout's line wrapping or user specified line breaks. That is, even if your input is a lengthy paragraph, without \n s, you will get the total lines as a result of internal line breaking algorithm.
Make sure editText.getLayout is not null before getting the line count. If it is null, you can call the measure() method on edittext.
Lines in a layout have 0 based index. So it starts from line 0 to line n-1 for n lines.
val offsetForHorizontal = editText.layout.getOffsetForHorizontal(lineCount-1, 0f)
This returns the Character start index / character offset for the given line.
The first param is the line you want to place the cursor. The second floating point is the x - offset where you want to place . the cursor over the specified line. Here, I'm giving 0f, so it places the cursor at the beginning of the line. If you want it at the end, then calculate the width of the line and provide this to your offset.
val lineWidth = editText.layout.getLineWidth(lineCount-1)
Now to set the Cursor at a particular line, use
Selection.setSelection(editText.text,offsetForHorizontal,offsetForHorizontal)
This is collapsed Cursor. If you need to select over a range of characters in a particular line, say 5 more characters, but only if you know the character count, you can do this
// assuming selection is at the beginning of the line
Selection.setSelection(editText.text,offsetForHorizontal,offsetForHorizontal+5)
However, you can get the character indices of line (start and end indices of the line in the entire text) as below
val startIndex = editText.layout.getLineStart(lineCount - 1)
val endIndex = editText.layout.getLineEnd(lineCount - 1)
P.S: Instead of moving to the last line, you can specify any line to set your selection in getOffsetForHorizontal()
Cursor moved to last line with 30 f offset(the nearest character at the give offset)
I think there is no direct method for that but n
th line means n
th occurrence of newline (\n
) so you can find that index and use setPosition
to it
In this function we use a while loop n
times using indexOf
to find the n
th newline occurrence
public static int ordinalIndexOf(String str, String substr, int n) {
int pos = str.indexOf(substr);
while (--n > 0 && pos != -1)
pos = str.indexOf(substr, pos + 1);
return pos;
}
And then we can use that index for setPosition
. Note that if we want to go to that line, we should go after \n
so we add +1
to the position
we've found
int n = 150;
if( n <= 0 ){
edittext.setSelection(0);
} else {
int position = ordinalIndexOf(edittext.getText().toString(), "\n", n);
if(position != -1)
edittext.setSelection(position + 1);
}