Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font resources not working properly with font when using Html texts

I was using calligraphy before the Font resources, one we see this we moved to Font resources and all works well,only one problem i am getting if my text view content is html and bold attribute(),its not highlighting, but if i remove the Font resources via xml and use the programmatic way it is working with same font.

i can give an example i am using Font resources like below

font_regular.xml

<?xml version="1.0" encoding="utf-8"?>
 <font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
    app:font="@font/myfont"
    app:fontStyle="normal"
    app:fontWeight="700"  />
</font-family>

My textview

  <TextView
        app:fontFamily="@font/font_regular"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="15sp"
        />

When i apply this to a html data contain bold,all data is coming from server as html data like example data as below

Data

<p>Hi i am <b>bold</b></p>

Expected result

Hi i am bold

But out put will be

Hi i am bold

The font is applying successfully but the portion of bold is not getting bold

But when i use programmatically via Typeface its working

Programmatically

 Typeface typeFace= Typeface.createFromAsset(context.getAssets(),
            "fonts/myfont.otf");

I google and gone through all the doc that i have seen via google none of them pointing me how to achieve this via Font resources.

Please help me to know whether i can achieve this via Font resources

like image 748
Ramz Avatar asked Dec 17 '25 03:12

Ramz


1 Answers

I used this code to achieve similar functionality in some apps and it worked really. I hope this could help

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    mDescTextView.setText(Html.fromHtml(myHtmlText, Html.FROM_HTML_MODE_COMPACT));
} else {
    Spanned myHtmlText = HtmlCompat.fromHtml(getContext(), myHtmlText, 0);
    mDescTextView.setText(myHtmlText);
}

Alright. If this is not working for you and as you mentioned in question font working when programmatically adding typeface, i have an alternative in that case. This may not be the exact answer you are looking for. But i think it will help in long run. No need to set typeface programmatically, neither have to add font face in xml. Using your own textview class will automatically load the font everywhere, also you can set thickness using the attributes (in values/attrs.xml) declared.

<com.youpackage.yourapp.view.customview.CustomTextView
    android:id="@+id/txt_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="24dp"
    android:hint="@string/label_full_name"
    app:customFontThickness="bold"
    android:inputType="textEmailAddress" />

For the custom textview class and attribute declaration see this. https://github.com/pramodpmk/CustomTextView

like image 82
Pramod Moolekandathil Avatar answered Dec 19 '25 20:12

Pramod Moolekandathil