Development issue/problem:

Is there a direct way to measure the height of the text?
Now I do it with Paint’s measureText() to get the width, then I try by trial and error to find the value to get an approximate height. I also made a joke with FontMetrics, but all these methods seem to be approximate and lame.

I’m trying to scale things up for different resolutions. I can do it, but the result is an incredibly detailed code with a lot of calculations to determine the relative size. I hate it! There’s gotta be a better way.

How can this problem be solved?

Decision No 1:

What about paint.getTextBounds() (object method)?

Decision No 2:

There are different ways to measure the height, depending on your needs.

Receiving text messages

If you do some kind of precise centering of a small amount of fixed text, you’ll probably want to get TexTVos. You can get this border rectangle.

Bounds of the rectangle = new Rect() ;
mTextPaint.getTextBounds(mText, 0, mText.length(), limitss) ;
int height = limitss.height() ;

As can be seen in the following figures, the different lines indicate different heights (shown in red).

Measuring text height to be drawn on Canvas ( Android ) –

This difference in height can be a disadvantage in certain situations where a constant height is simply necessary, regardless of the text. See next section.

Metric writing.

You can calculate the height of the font using the font metric. The height is always the same because it depends on the font and not on a specific text string.

Paint.FontMetrics fm = mTextPaint.getFontMetrics();
suspended height = fm.descent – fm.ascent ;

The baseline is the line on which the text is located. The descent is generally the descent furthest from the board below the line, and the climb is generally the descent furthest from the board above the line. To obtain the height, subtract the climb because it has a negative value. (Baseline y=0 and y suppresses the screen upwards).

Look at the next picture. The height of the two strings is 234,375 metres.

Measuring text height to be drawn on Canvas ( Android ) –

If you want not only the height of the text, but also the height of a line, you can proceed as follows:

floating height = fm.bottom – + fm.leading; // 265.4297

It’s up and down. The master (line spacing) is normally zero, but you have to add it anyway.

The pictures above are from this project. You can play with it to see how Font Metrics works.

Static control

To measure the height of a multipart text, you must use StaticLayout. In this answer I have described it in detail, but the most important way to reach this height is as follows:

Text string = This is a text type. It’s kind of a text. It’s kind of a text. It’s kind of a text. It’s kind of a text. It’s kind of a text;

TextPaint myTextPaint = new TextPaint() ;
myTextPaint.setAntiAlias(true) ;
myTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density) ;
myTextPaint.setColor(0xFF0000) ;

int width = 200 ;
Layout.Alignment.ALIGN_NORMAL ;
float distanceMultiplier = 1 ;
float spaceAddition = 0 ;
boolean includePadding = false ;

StaticLayout myStaticLayout = new StaticLayout (Text, myTextPaint, width, alignment, distance multiplication, distanceAddition, includePadding) ;

floating height = myStaticLayout.getHeight() ;

Decision No 3:

The @bramp response is correct – partly because it does not mention that the calculated limits will be the minimum rectangle that contains the text completely with the implicit start coordinates 0, 0.

This means, for example, that the height of Py is different from the height of Py or hi or oi or aw, because they require different heights in pixels.

This is by no means the equivalent of FontMetrics in classic Java.

The width of the text is not so annoying, but the height is.

Especially if you need to center the drawn text vertically, try to keep the text boundaries a (without the quotes) instead of using the text to be drawn.
It works for me…

My point is this:

Color = new color (Color.ANTI_ALIAS_FLAG | Color.LINEAR_TEXT_FLAG) ;

paint.setStyle (Paint.Style.FILL);
paint.setColor (Color);
paint.setTextAlign (Paint.Align.CENTER);
paint.setTextSize (TextSize);

Rect boundaries = new Rect() ;
paint.getTextBounds(a, 0, 1, limits) ;

buffer.drawText(this.myText, canvasWidth >> 1, (canvasHeight + limits.height()). >> 1, paint);
// remember that x >> 1 corresponds to x / 2, but works much faster

Vertical text alignment is the alignment of the center of the boundary box, which is different for different types of text (titles, long letters, etc.). But in reality we also want to level the source code so that it doesn’t look elevated or grooved. Thus, as long as we know the middle of the smallest letter (e.g. a), we can reuse its alignment for other texts. This allows you to center and align all texts on the baseline.

Decision No 4:

The height is the size of the text you specify in the Paint variable.

Another way to know the altitude

mPaint.getTextSize() ;

Decision No 5:

You can use the android.text.StaticLayout class to set the required limits and then call getHeight(). You can draw text (on layouts) by calling the drawing method (Canvas).

Decision No 6:

The text size for the Paint object can easily be determined with the getTextSize() method.
For example:

Paint mTextPaint = new paint (Paint.ANTI_ALIAS_FLAG) ;
// Use the density multiplier to account for different pixel densities
last floating point density multiplier = getContext().getResources()
.getDisplayMetrics().density ;
mTextPaint.setTextSize(24.0f*density multiplier) ;


Floating point size = mTextPaint.getTextSize() ;

Decision No 7:

You must use Rect.width() and Rect.height() returned by getTextBounds(). That’s fine with me.

Decision No 8:

If anyone has a problem again, it’s my code.

I have a custom view that is square (width = height) and I want to assign a symbol to it. onDraw() shows you how to get the height of the symbol, although I don’t use it. The symbol is shown in the middle.

The public SideBarPointer class extends the view {

TAG = SideBarPointer ;

private context;
private string name = ;
private width int;
private height int

public SideBarPointer(context pointer) {
this.context = context;

public SideBarPointer(context, AttributeSet attrs) {
super(context, attrs);
this.context = context;

public SideBarPointer(context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;

private empty init() {
// setBackground color(0x64FF0000);

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec) ;

Height = this.getMeasuredHeight() ;
Width = this.getMeasuredWidth() ;

SetMeasuredDimension (width, width);

protected blank onDraw(Canvas canvas) {
float mDensity = context.getResources().getDisplayMetrics().density ;
float mScaledDensity = context.getResources().scaledDensity().getDisplayMetrics ;

ColorpreviewPaint = new Paint() ;
previewPaint.setColor(0x0C2727) ;
previewPaint.setAlpha(200) ;
previewPaint.setAntiAlias(true) ;

PaintPreviewTextPaint = new paint();
previewTextPaint.setColor (Color.White);
previewTextPaint.setAntiAlias (true);
previewTextPaint.setTextSize(90 * mScaledDensity);
previewTextPaint.setShadowLayer(5, 1, 2, Color.argb(255, 87, 87));

float previewTextWidth = previewTextPaint.measureText(label) ;
// float previewTextHeight = previewTextPaint.descent() – previewTextPaint.ascent() ;
RectF previewRect = new RectF(0, 0, width) ;

canvas.drawText(label, (width – previewTextWidth)/2, – previewTextPaint.ascent(), previewTextPaint) ;


public invalid setLabel (String label) {
this.label = Label;
Log.e(TAG, Label: + Label) ;


Good luck!

agorapulse competitor analysis,agorapulse review,eclincher,later reviews,zoho social,sendible,sprout social free alternative,sprout social competitor analysis,traject social app,e-clincher,buffer vs sprout social,mavsocial,heropost review,buffer business login,socialbee vs hootsuite,socialpilot social media,social pilot app,social pilot free trial,Is Agorapulse the best tool?,Maybe not! It is advisable to decide your social media marketing goals and then choose the right product. Agorapulse lacks in terms of providing integrations, unified feeds, unified reports, asset library, animated GIF support, etc. Also, Agorapulse does not offer a free plan to get hands-on experience!,What are the best Agorapulse alternatives?,SocialPilotHootSuiteSprout SocialSendibleBufferMeet EdgarSocial BakersCoScheduleeClincherZoho Social,Is SocialPilot better than Agorapulse?,It depends on what your requirements are. But, SocialPilot is a better social tool than Agorapulse. It can be used to publish on 9 social media platforms seamlessly whereas Agorapulse has limited capabilities in terms of letting you do the same. SocialPilot has all the requisite features to make your social media marketing success! The company offers technical support 5 days a week for all of its clients. If you require a high level of support, with a dedicated account manager and telephone support, then Agorapulse is not for you. In this case, you should opt for SocialPilot! For enterprise accounts, SocialPilot designates a full-time customer support manager.,instagress sign up,socialsteeze,free instagress alternative,archie co alternative,follow adder alternative,stellation media reddit,hootsuite,sprout social,buffer

You May Also Like

Airtel Balance Check Numbers and Offers

Are you an Airtel customer? Do you know how to check Airtel’s…

The Best Model Train Books

It’s rather shocking that we like model trains. And no matter how…

Try something that can’t be SIM swapped • The Register

On Tuesday, Microsoft Corporation advised Internet users to accept multi-factor authentication (AMF)…

509 Delta R3 Helmet Review: Ignite & Fidlock Technology

Snowmobiles are an exciting activity that can take a dangerous turn if…