1

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

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Hi @jeprubio, i am showing it in a tooltip, so when i set the typeface, holesTextView.setTypeface(typeface) is not reachable. I updated my code with tooltip function. Could you plz check it
Are you using this lib github.com/tomergoldst/tooltips ? it has a setTypeface() method. Check my update
No @jeprubio, github.com/ViHtarb/Tooltip. This is the lib i am using
There is also a .setTypeface() method in there in the Tooltip.Builder, have you checked?
yea i check that and now it works i guess. Thanks jeprubino :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.