Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edittext line number and currentline cursor position.

Tags:

android

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

like image 396
sarath Avatar asked Mar 21 '12 12:03

sarath


2 Answers

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();
like image 168
ρяσѕρєя K Avatar answered Oct 22 '22 02:10

ρяσѕρєя K


If you are not using some fixed width font as courrier, it is not easy.

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

you should measure the text, as 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 for the search of the correct position some hashing algorithm

like image 30
Gangnus Avatar answered Oct 22 '22 02:10

Gangnus