``` private val typeface = Typeface.create(Typefac...
# getting-started
u
Copy code
private val typeface = Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD)

class InitialsDrawable(val context: Context) : Drawable() {
    private val textPaint = Paint().apply {
        isAntiAlias = true
        color = context.getAttr(R.attr.defaultTextColor)
        textSize = context.resources.dpToPixels(11F)
        typeface = @file:typeface <--------------
        textAlign = Paint.Align.CENTER
    }
s
when this code is in the file
MyKotlinFile.kt
, then you can access the top level val with
MyKotlinFileKt.typeface
.
every file that has more than one class in it gets its own class namend
<nameOfTheFile>Kt
that has the top level members as static fields
but generally, you should avoid shadowing variables if it is possible. in this code above you could do it like this:
Copy code
private val DEFAULT_TYPE_FACE = Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD)

class InitialsDrawable(val context: Context) : Drawable() {
    private val textPaint = Paint().apply {
        isAntiAlias = true
        color = context.getAttr(R.attr.defaultTextColor)
        textSize = context.resources.dpToPixels(11F)
        typeface = DEFAULT_TYPE_FACE <--------------
        textAlign = Paint.Align.CENTER
    }
💯 2