Explain Codes LogoExplain Codes Logo

Android Center text on canvas

android
canvas
paint
text-centering
Alex KataevbyAlex Kataev·Nov 19, 2024
TLDR

To center text on an Android canvas, calculate coordinates (X and Y) for drawText() using the Paint class to measure the text as shown:

Paint paint = new Paint(); paint.setTextSize(50); // Adjust this for your needs paint.setAntiAlias(true); // For a smoother text rendering paint.setTextAlign(Paint.Align.CENTER); // "CENTER" is a crowd-pleaser! float xPos = canvas.getWidth() / 2; // Half of canvas width, simple! // Subtract the ascent and descent from canvas height, then halve it. Why make things complicated? float yPos = (canvas.getHeight() - (paint.descent() + paint.ascent())) / 2; paint.setColor(Color.BLACK); // Because black is the new black canvas.drawText("Centered Text", xPos, yPos, paint); // Finally, draw that precious text

This quick solution lines up text both horizontally and vertically on the canvas. Remember to adapt paint properties to fulfill your design goals.

The how and why of Paint.Align class

To get a precise horizontal center while working with drawText(), set Paint.Align parameter to CENTER. This will save our time otherwise wasted on adjusting little here and there every time canvas changes.

The measurement saga: Paint.measureText()

For accurate center placement of text, apply Paint.measureText(). This sneaky function helps us win the race against skewness, as it returns how wide your text is based on the Paint style.

Better speed ahead with pre-allocation

Preallocate objects like Paint and Rect outside the onDraw() method. It's a performance-boost for those who hate waiting on their apps. (So, basically, everyone?)

Positioning: It's all about location

Using a Rect or RectF to calculate bounds improves accuracy in placing the text within the canvas. Now our seemingly wayward text can find its true home!

Achieving the aesthetic perfection

If you wish to render your text as elegantly as a brushed-up gentleman, enable antialiasing by invoking paint.setAntiAlias(true). You're welcome!

Consistency in visual appeal

Decide on a custom typeface that suits your taste, and use paint.setTypeface() and paint.setTextSize() to achieve uniformity throughout your app. No more wardrobe crisis!

Using adjustments to your advantage

On occasions where text needs a slight nudge, make the requisite adjustments factoring in width and height of the text lined up against the canvas dimensions. Sometimes it's the tiny tweaks that make all the difference.

Into the wild: Meeting challenges

When centering text, the path isn't always rosy. Here's some help to pluck those thistles:

  • Visual aids: Struggling with text boundaries while debugging? Why not outline them by setting Paint to STROKE?

  • Clipping blues: Use Canvas.getClipBounds(Rect) to chill about text being clipped off with varying canvas dimensions. Now, clip your worries and throw them away!

  • Translation to the rescue: Use Canvas translation to adjust the position of text by just the right amount. It's like the GPS of canvas!

Real-world benchmarking for perfect centering

Centering is a balancing act. To truly appreciate your efforts in perfect centering, ensure to take screenshots post-editing. It's just like mirror selfies but for your app.

Effective handling of paint colors

Use paint.setColor() to set the color of your text. But wait, this isn't a one-trick pony! You can set the background color with it too. Color me surprised!