Edittext line number and current cursor position.

Now I am working on an Android application. I created a custom keyboard with functionality. I use edittext to display the entered texts. Editing text can have n number of lines. Now my problem is that I have an up button on my keyboard. So if I press the up button, then I need to go to the previous lines with the same position. But I could not find out the edittext line number and cursor position of the current line. Please help me with friends.

+6
source share
2 answers

for the current cursor line try the following:

public int getCurrentCursorLine(EditText editText) { int selectionStart = Selection.getSelectionStart(editText.getText()); Layout layout = editText.getLayout(); if (selectionStart != -1) { return layout.getLineForOffset(selectionStart); } return -1; } 

and to use the cursor position getSelectionStart() :

 int cursorPosition = myEditText.getSelectionStart(); 
+7
source

If you do not use a fixed-width font as a courier, this is not easy.

There is no function that returns the position of letters for the source text and the required width in pixels. You will have to write it.

you should measure the text like here:

 paint.setTypeface(Typeface.DEFAULT);// your preference here paint.setTextSize(33); // have this the same as your text size String text = "get text here "; paint.getTextBounds(text, 0, text.length(), bounds); 

I would use some hash algorithm to find the right position

+2
source

Source: https://habr.com/ru/post/911294/


All Articles