In my case, I didn't have to put the text in the middle of a canvas, but in a wheel that spins. Though I had to use this code to succeed:
fun getTextRect(textSize: Float, textPaint: TextPaint, string: String) : PointF {
val rect = RectF(left, top, right, bottom)
val rectHeight = Rect()
val cx = rect.centerX()
val cy = rect.centerY()
textPaint.getTextBounds(string, 0, string.length, rectHeight)
val y = cy + rectHeight.height()/2
val x = cx - textPaint.measureText(string)/2
return PointF(x, y)
}
Then I call this method from the View class:
private fun drawText(canvas: Canvas, paint: TextPaint, text: String, string: String) {
val pointF = getTextRect(paint.textSize, textPaint, string)
canvas.drawText(text, pointF!!.x, pointF.y, paint)
}