How can I calculate the position X , Y of a letter in a TextView
by knowing it's index?
I need this to show a graphical pointer to the touched letter or word but I don't know how to locate the right place.

- 37,241
- 25
- 195
- 267

- 2,343
- 2
- 31
- 43
-
if you know the index, then maybe x = (Paint.measureText(yourText) / yourText.length()) * index – Boris Mocialov Aug 05 '13 at 07:29
-
is it a one-line TextView or can be more than one line? – Boris Mocialov Aug 05 '13 at 07:31
-
It's big TextView with so many lines ;) – Soheil Setayeshi Aug 05 '13 at 07:32
-
1y = http://stackoverflow.com/questions/9325798/android-textview-and-getting-line-of-text – Boris Mocialov Aug 05 '13 at 07:35
-
1This is a good question. Shoudnt have been marked negative. Let me know if anybody get an answer to this. – Srujan Barai Nov 13 '15 at 06:00
2 Answers
May this help you:
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...

- 3,189
- 2
- 22
- 30
Try this:
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();
That should give you the height and width. Create an array of height/widths with a different value for each individual character, you could do it quickly if you used a loop and ASCII character codes. Then, you could add the widths of each character until you get to the one you want.
But there's what I think is an easier way. Instead of a TextView, try a LinearLayout with multiple textviews. If you use a textview for each character, you can just use getX and getY.