Android and calculating the size of a single line in a given font and font size?

Is there an API method for calculating the size (i.e. width and height) of a string displayed on one line in a given font and font size?

+7
source share
3 answers
Paint p = new Paint(); p.setTypeface(TypeFace obj); // if custom font use `TypeFace.createFromFile` p.setTextSize(float size); float textWidth = p.measureText("Your string"); // you get the width here and textsize is the height. 
+8
source
 Paint mPaint = new Paint(); mPaint.setTextSize(/*put here ur size*/); mPaint.setTypeface(/* put here ur font type */); Rect bounds = new Rect(); mPaint.getTextBounds(text, 0, text.length(), bounds); 

then make a call

 bounds.width(); bounds.height(); 
+8
source

The Paint class has a measureText () method that will give you the width in float

 Paint p = new Paint(); int INFO_WINDOW_WIDTH = (int)p.measureText("this is string"); 

and another Paint.getTextBounds

+2
source

All Articles