android kotlin - Canvas center text

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.widget.ImageView


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val imageView = findViewById<ImageView>(R.id.imageView)


        // draw text on canvas center and get bitmap
        val bitmap = drawCenterAlignText(
            text = "Center Text",
            textSize = 150F,
            textColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


        // show drawing on image view
        imageView.setImageBitmap(bitmap)
    }
}



// function to draw text on canvas center
fun drawCenterAlignText(
    text:String = "Text To Draw",
    textColor:Int = Color.BLACK,
    textSize:Float = 200F,
    canvasColor:Int = Color.WHITE
):Bitmap{
    val bitmap = Bitmap.createBitmap(
        1500,
        750,
        Bitmap.Config.ARGB_8888
    )


    // canvas to draw the text
    val canvas = Canvas(bitmap).apply {
        drawColor(canvasColor)
    }


    // paint to draw the text
    val paint = Paint().apply {
        isAntiAlias = true
        color = textColor

        // paint's text size in pixel units
        this.textSize = textSize

        // set typeface with style
        typeface = Typeface.create(Typeface.SERIF,Typeface.BOLD)

        // important to align text horizontally center
        textAlign = Paint.Align.CENTER
    }

    // calculate center position to draw text on canvas
    val x = canvas.width / 2F
    val y = (canvas.height /2 ) -
            (paint.descent() + paint.ascent()) / 2

    // finally, draw the text on canvas center
    // both horizontally and vertically centered text
    canvas.drawText(
        text, // text to draw
        x, // x-coordinate of the origin of the text being drawn
        y, // y-coordinate of the baseline of the text being drawn
        paint // paint used for the text e.g. color, size, style
    )

    return bitmap
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials