0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Soheil Setayeshi
  • 2,343
  • 2
  • 31
  • 43

2 Answers2

1

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

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
1

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.