0

My case is same as Android - three sentences, two styles, one TextView. The only difference is that I want to set a Custom Font on the whole text. I have included roboto Font as an assets in my project and want to apply that font to the TextView with first part of the text will be roboto_LIGHT and middle part roboto_NORMAL and remaining part roboto_LIGHT again. Any suggestions how it can be done ?

Text needed in following format. Custom text with different styles and single textview.

screenshot

Mohammed Qadah
  • 75
  • 1
  • 10

1 Answers1

0

You can use some html tags like <b> for bold with the following code:

textView.setText(Html.fromHtml("Login to the <b>Web Wallet</b> on PC or MAC"));

Then you can define your custom font family as in How to create custom font family with bold font

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

The weight 700 is the font for bold style. And then you should apply that created font family to the TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:fontFamily="@font/my_custom_font"/>

Or you can add 3 TextViews in a ConstraintLayout and give them some constraints to be in the positions you want otherwise.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • Thanks for your help, i wrote exactly what you wrote but the font doesn't change it's showing only normal with bold but doesn't show roberto light – Mohammed Qadah Mar 26 '20 at 12:21
  • It seems as it's not loading that font family for whatever reason if you see some text in bold. Try with `app:fontFamily` instead of `android:fontFamily` in the TextView. Or try with both and then if it works you can remove the one which doesn't apply – jeprubio Mar 26 '20 at 12:30