sarath sarath - 6 months ago 84
Android Question

Edittext line number and currentline cursor position.

Now I am working on a Android application. I created a custom keyboard with functionalities. I am using a edittext for displaying the entered texts. Edit text may have n number of lines.Now my problem is I have a up button in my keyboard.So if I click the up button then i have to go the previous lines same position.But I couldn't able to find out the edittext line number and curser position of the currentline. Please help me friends

Answer

for current Cursor Line try this:

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 for Cursor position use getSelectionStart():

int cursorPosition = myEditText.getSelectionStart();