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

I am not aware of a simple direct way to do this but you should be able to put something together using the Paint object of the TextView via a call to TextView.getPaint() Once you have the paint object you will have access to the underlying FontMetrices via a call to Paint.getFontMetrics() and have access to other functions like Paint.measureText() Paint.getTextBounds(), and Paint.getTextWidths() for accessing the actual size of the displayed text.

Thanks, I ended up using this. Too bad there's no simple "pointToCharacterIndex" type of method built-in, but it wasn't rocket science to roll my own thanks to Paint. – Epaga Feb 22 '10 at 15:44 @Tony Blues answer has the pointToCharacterIndex sort of thing you're looking for.

– James Moore Jul 6 at 20:21.

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...

I asked the Google engineers at Google IO office hours, and this is the same thing they came up with. – James Moore May 10 at 4:57.

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?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions