Jayadev
Jayadev

Reputation: 201

How to find the text width from android canvas?

I am trying to find the height and width of text already drawn into the canvas. For finidng the height there is function:
getTextHeight(String text,Paint paint);
But there is no function called getTextWidth or something like that. Why is that? And what is the possible way to find the text width?

Upvotes: 1

Views: 1080

Answers (2)

br00
br00

Reputation: 1454

int mTextHeight;
Rect textBounds = new Rect();
paintText.getTextBounds(text, 0, text.length(), textBounds);
mTextHeight = textBounds.height();

Upvotes: 0

ripple182
ripple182

Reputation: 861

I'm not sure you can measure a string that has already been drawn to the canvas, but I use

paint.measureText(string)

to determine the width of text. Make sure to call paint.setTypeface() and paint.setTextSize() before calling the measureText method to get the accurate width.

Upvotes: 1

Related Questions