Press enter to see results or esc to cancel.

Lesson 35 – Draw: text in kotlin

Share this

Another resource that allows the Canvas class is to plot text.

1 – We created a project called: Project039

We delete the TextView that automatically adds the Android Studio and define the ConstraintLayout id with the value: layout1:


Lesson 35 - Draw: text in kotlin - Lesson 35 - Draw: text in kotlin - Lesson 35 - Draw: text in kotlin -

Now we encode the class in Kotlin where all logic is found:

package com.coding180.project039

import android.content.Context
import android.os.Bundle
import android.view.View

class MainActivity: AppCompatActivity () {

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

        val layout1 = findViewById ( as
        val background = Canvass (this)
        layout1.addView (background)
    class Canvass (context: Context): View (context) {

        override fun onDraw (canvas: Canvas) {
            canvas.drawRGB (0, 0, 255)
            val brush1 = Paint ()
            brush1.setARGB (255, 255, 0, 0)
            brush1.setTextSize (30f)
            brush1.setTypeface (Typeface.SERIF)
            canvas.drawText ("Hello World (SERIF)", 0f, 70f, brush1)
            brush1.setTypeface (Typeface.SANS_SERIF)
            canvas.drawText ("Hello World (SANS SERIF)", 0f, 100f, brush1)
            brush1.setTypeface (Typeface.MONOSPACE)
            canvas.drawText ("Hello World (MONOSPACE)", 0f, 140f, brush1)
            var tf = Typeface.create (Typeface.SERIF, Typeface.ITALIC)
            brush1.setTypeface (tf)
            canvas.drawText ("Hello World (SERIF ITALIC)", 0f, 180f, brush1)
            tf = Typeface.create (Typeface.SERIF, Typeface.ITALIC or Typeface.BOLD)
            brush1.setTypeface (tf)
            canvas.drawText ("Hello World (SERIF ITALIC BOLD)", 0f, 220f, brush1)

To graph text we have the drawText method that allows us to print a String in a certain column, row with a specific brush that we can define its color:

            Val brush1= Paint ()
            brush1.setARGB (255, 255, 0, 0)

The size of the letter:

            brush1.setTextSize (30f)

The style of the letter:

            brush1.setTypeface (Typeface.SERIF)

The program interface is:


Lesson 35 - Draw: text in kotlin - Lesson 35 - Draw: text in kotlin - Lesson 35 - Draw: text in kotlin -

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this