Android Center text on canvas
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Centering text on an Android Canvas is harder than centering a rectangle because drawText uses a baseline, not the top or middle of the glyph box. The correct solution is to center horizontally with Paint.Align.CENTER and compute the vertical baseline from the font metrics.
Understand the Baseline First
The Android Canvas documentation states that the y argument of drawText is the baseline of the text being drawn. That means height / 2f is not the vertical center of the visible text, even though it looks like the obvious coordinate to use.
This often draws the text slightly low because the baseline sits below the top of most letters and above descenders. So the problem is not "How do I draw text at the center?" but "How do I compute the baseline that makes the text appear centered?"
Horizontal Centering Is Straightforward
For horizontal placement, configure the Paint object to center text on the x coordinate.
Once textAlign is CENTER, drawing at width / 2f centers the text horizontally. There is no need to manually subtract half the measured width for a single line if the paint alignment is already correct.
Compute Vertical Centering with Font Metrics
For vertical centering, use the font's ascent and descent. Android exposes these values through Paint.FontMetrics.
This is the standard single-line centering formula. It works because the midpoint between ascent and descent tells you how far the baseline should be shifted so the drawn glyph area is visually centered.
Put the Logic in a Custom View
Encapsulating the centering code in a custom view keeps the drawing behavior stable and easy to reuse.
This pattern is a good fit for badges, score displays, splash labels, and other simple custom-drawn text.
Multiline Text Needs a Different Approach
The font-metrics formula above is for one line. If the text can wrap, use a text layout object and center the whole text block instead of centering a single baseline.
That centers the entire paragraph block vertically and horizontally rather than trying to force one-line math onto wrapped text.
Debugging Centering Problems
If the result still looks wrong, draw guide lines through the middle of the canvas. This helps separate incorrect math from font-specific optical effects.
Some fonts are technically centered by metrics but still look slightly off because of glyph shapes. Guide lines make that easier to diagnose.
Common Pitfalls
- Treating the
yargument ofdrawTextas the visible vertical center instead of the baseline. - Measuring text width and assuming that solves vertical centering too.
- Applying the single-line formula to multiline or wrapped text.
- Recreating
Paintobjects insideonDrawinstead of reusing them. - Forgetting to call
invalidate()when the text changes.
Summary
- '
Canvas.drawTextuses a baseline for itsycoordinate.' - Use
Paint.Align.CENTERto center text horizontally. - Use ascent and descent from
Paint.FontMetricsto compute the centered baseline. - Use a layout object for multiline centered text.
- Draw guide lines if you need to verify whether the centering math is actually correct.

