androidhtmlkotlinfromhtml

How to add font style to textview android kotlin


I need help, i am stuck in this. My requirement is, I need to add text inside a tooltip. I did that actually. But the text include multiple line text also some lines having bold style other line having regular style with custom font style. By the below code which i am using, i can able to add bold, italic and regular style. But i need to add below custom font style into the text. Please have a look into my below code.

textViewToShow.setOnClickListener { view ->

    val tfBold = Typeface.createFromAsset(
        context!!.assets,
        "fonts/myriad_pro_bold.ttf"
    )

    val tfRegular = Typeface.createFromAsset(
        context!!.assets,
        "fonts/myriad_pro_regular.ttf"
    )

    var string1 = “Item show in Bold style”
    var string2 = “Item show in Regular style”
    var string3 = “Item show in Regular style with item description”

    val holesTextView = Html.fromHtml("<b>" + string1 + "</b> <br><br> <b><i>"+ string2 + "</i></b> " + string3)
    showToolTip(view, holesTextView)

}

    private fun showToolTip(view: View, holesTextView: Spanned) {
        val builder: Tooltip.Builder =
            Tooltip.Builder(view, R.style.Tooltip)
                .setText(holesTextView)
        builder.show()
    }

Any solution/suggestions are highly appreciated


Solution

  • I would create a font family

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/myriad_pro_regular" />
        <font
            android:fontStyle="italic"
            android:fontWeight="400"
            android:font="@font/myriad_pro_italic" />
        <font
            android:fontStyle="normal"
            android:fontWeight="700"
            android:font="@font/myriad_pro_bold" />
    </font-family>
    

    following the answer of How to create custom font family with bold font for the bold font.

    And then you can assign the font family in the layout:

    <EditText
        android:fontFamily="@font/mycustomfont"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, World!" />
    

    Or Programatically:

    val typeface = ResourcesCompat.getFont(context, R.font.mycustomfont)
    holesTextView.setTypeface(typeface)
    

    Or in the case of a tooltip using its setTypeface() method:

    val typeface = ResourcesCompat.getFont(context, R.font.mycustomfont)
    val builder: Tooltip.Builder = Tooltip.Builder(view, R.style.Tooltip)
        .setText(holesTextView)
        .setTypeface(typeface)
        builder.show()
    

    And use HtmlCompat.fromHtml as Html.fromHtml is deprecated:

    val holesTextView = HtmlCompat.fromHtml("<b>" + string1 + "</b> <br><br> <b><i>"+ string2 + "</i></b> " + string3, HtmlCompat.FROM_HTML_MODE_LEGACY)