Uwais A
Uwais A

Reputation: 737

Finding coordinates of a character?

I have a multi-line TextView. Is there a way to get the x coordinate of the pixel to the left of a character e.g. the 3rd character? In my case it will always be the 3rd character, but a general solution would be nicer.

I have the y coordinate of the pixel just above the character using:

Layout textViewLay = mTextView.getLayout();
int posY = textViewLay.getLineTop(0);

but the x coordinate has me stumped. Any ideas? I'm probably missing something really simple.

Upvotes: 2

Views: 1324

Answers (1)

Cuong Huynh
Cuong Huynh

Reputation: 159

Try to use this code:

Rect bounds = new Rect();
Paint tpaint = textView.getPaint();
tpaint.getTextBounds(text,0,2,bounds);
int height = bounds.height();
int width = bounds.width();

Upvotes: 5

Related Questions