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
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With