How to get string width on Android?

Mikael Lindlöf picture Mikael Lindlöf · Sep 2, 2010 · Viewed 71.9k times · Source

I would like to get height too if possible.

Answer

Frank picture Frank · Sep 2, 2010

You can use the getTextBounds(String text, int start, int end, Rect bounds) method of a Paint object. You can either use the paint object supplied by a TextView or build one yourself with your desired text appearance.

Using a Textview you Can do the following:

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