3

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

fdelafuente
  • 1,114
  • 1
  • 13
  • 24
Ramz
  • 7,116
  • 6
  • 63
  • 88
  • Can you please update the code you are using to set text? Also clarify are you testing it on device running Marshmallow or lower versions? – Pramod Moolekandathil Jun 27 '19 at 12:34
  • @Pramod Moolekandathil The code i are using to set text is the html text i had given in the question itself i marked it as Data for you clarity this is the text

    Hi i am bold

    , i run in all device android pie,marshmallow,oreio,even in gingerbread, font is applying but the area i need to get bold is not setting
    – Ramz Jun 27 '19 at 12:39
  • I guess the trick is to use a `font-family`: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml – Phantômaxx Jun 27 '19 at 13:16
  • i also had the same problem, final stick with programmatic way and used calligraphy – Samwinishere Here Jun 28 '19 at 05:47

2 Answers2

2

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

  • i am setting all properly same as above – Ramz Jun 27 '19 at 12:54
  • Ok. I have an alternative that could help you. Instead of using TextView, use a custom one by extending TextView class. Add stylable attributes in attrs.xml file. Then use it inside you layout xml. I have updated the answer. Please check. – Pramod Moolekandathil Jun 28 '19 at 05:32
-1

Add this

String str = Hi i am + "" + bold + " ";
tv_view.setText(Html.fromHtml(str));

Ravi Soni
  • 68
  • 5
  • But the content is html for example as i give in the question it is

    Hi i am bold

    and it not a static data its coming from server as html
    – Ramz Jun 27 '19 at 12:41