Cómo obtener la posición del cursor (x, y) en EditText android
¿Cómo obtengo la posición del cursor x, y en un EditText
en Android? (Donde x es la línea # yy es la columna #)
int pos = editText.getSelectionStart(); Layout layout = editText.getLayout(); int line = layout.getLineForOffset(pos); int baseline = layout.getLineBaseline(line); int ascent = layout.getLineAscent(line); float x = layout.getPrimaryHorizontal(pos); float y = baseline + ascent;
Y allí se obtienen las posiciones x, y en píxeles del cursor.
Puede encontrar el número de línea y la columna de la selección actual así:
// Get the current selection's line number int line = edittext.getLayout().getLineForOffset(edittext.getSelectionStart()); // Find the index for the start of the selected line. Subtract that from the current selection's index int column = edittext.getSelectionStart() - edittext.getLayout().getLineStart(line);