36 – Draw: text with external font style in kotlin

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

We can build applications and incorporate external True Type fonts. For this, we will implement an application that shows a text with an external source.

1 – We created a project called: Proyecto040

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

 

36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin -

First we have to create a folder where we will store our external sources. To do this press the right mouse button on the app and select New-> Folder-> Assets Folder:

 

36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin -

A dialog appears and press Finish. We have already created the Assets folder where we can store the fonts.

We are now downloading a font from the Creamundo site and copying the source file to the assets folder as shown (remember copy/paste works in Android Studio):

 

36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin -

Now we encode the class where the whole logic is:

package com.coding180.project040



import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.graphics.Typeface
import android.view.View


class MainActivity: AppCompatActivity () {
// coding180.com
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)

        val layout1 = findViewById (R.id.layout1) as android.support.constraint.ConstraintLayout
        val background = Canvass (this)
        layout1.addView (background)
    }

    class Canvass (context: Context): View (context) {

        override fun onDraw (canvas: Canvas) {
            canvas.drawRGB (255, 255, 255)
            val brush1 = Paint ()
            brush1.setARGB (255, 0, 0, 0)
            brush1.setTextSize (80f)
            val face = Typeface.createFromAsset (getContext (). getAssets (), "Acquaintance.ttf")
            brush1.setTypeface (face)
            canvas.drawText ("I love Coding180", 0f, 120f, brush1)
        }
    }
}

In the onDraw method we create a source by calling the createFromAsset method and indicating the name of the file that we download from the internet (in the second parameter, name the file you chose and downloaded from the internet):

            Val face = Typeface.createFromAsset (getContext (). GetAssets (), "Acquaintance.ttf")
            brush1.setTypeface (face)

The result on the screen is:

36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin - 36 - Draw: text with external font style in kotlin -

 

…………

About The Author

Related posts

Leave a Reply