Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to determine character index of a touch event's position in TextView?

I have a TextView with an OnTouchListener. What I want is the character index the user is pointing to when I get the MotionEvent. Is there any way to get to the underlying font metrics of the TextView?

like image 678
Epaga Avatar asked Feb 20 '10 16:02

Epaga


1 Answers

Have you tried something like this:

Layout layout = this.getLayout();
if (layout != null)
{
    int line = layout.getLineForVertical(y);
    int offset = layout.getOffsetForHorizontal(line, x);

    // At this point, "offset" should be what you want - the character index
}

Hope this helps...

like image 109
Tony Blues Avatar answered Sep 23 '22 23:09

Tony Blues