sarath sarath - 1 year ago 228
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 Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download