Member-only story
Expounding Android Canvas’ DrawText

In Android, when we want to have some Text, we would just use TextView. However, in case we want to have a custom view and have better control of the text, we could use the Canvas
' drawText
api to draw the text.
Check out the below simple App I made to show some fun with drawText (you could get the code in the below link)

In this blog, I’ll be explaining some drawText
features provided.
The function arguments
It consist of 4 arguments.
canvas.drawText(text, coordinateX, coordinateY, paint)
- The
text
is just the text instring
. There are other similar function which takes incharArray
andcharSequence
. - The
coordinateX
andcoordinateY
is use to position the text. This is further explained below in the next section - The
paint
to decide how one would draw the text.
The coordinate
If we were to draw the text using coordinate 0, 0 as below
canvas.drawText(text, 0, 0, paint)
We’ll not see the text shown at all. This is because the coordinate is set as below

Y coordinate
This is set at the base line of the text (not the most bottom, as you notice it is not at the bottom tip of the y
letter, but below all other letters that doesn’t have a ‘tail’).
X coordinate
The X coordinate is by default set on the left side of the text. This is because by default the text is left aligned. One could set to be Center Align or Right Align through the paint as shown below.
paint.textAlign = Paint.Align.CENTER
paint.textAlign = Paint.Align.RIGHT
paint.textAlign = Paint.Align.LEFT // Default