2

I want to add custom bold font in my font_family.xml. I did not find bold fontStyle="bold" in font family developer doc. It only have normal or italic attribute. Here is font_family.xml

<?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/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

So i have to use style like

<style name="fontBoldStyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/montserrat_semi_bold</item>
</style>

or like this

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

My question

  1. What does fontStyle attribute do? Even i did not provide italic font to font_family.xml, and set textStyle="italic" then my font is italic.
  2. how to add custom bold font in font family and set android:fontStyle="bold" in font_family.xml so i dont have to create different styles and i just have to set textStyle="bold"

So what is a solution that i can set 3 of my fonts - normal, bold, italic to font_family.xml, and set android:fontFamily="@font/font_family" on TextView and then i can use android:textStyle="bold" or italic, or normal?

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Use the Link [Bold in font Family](https://stackoverflow.com/questions/12128331/how-to-change-fontfamily-of-textview-in-android). i think its work. – ibad ur rahman Apr 27 '18 at 06:17
  • I have custom bold font.ttf so i dont use textStyle = "bold" – Khemraj Sharma Apr 27 '18 at 06:18
  • @IbadUrRahman i have already searched lot before asking, my question is not related to that. Thanks for suggestion – Khemraj Sharma Apr 27 '18 at 06:22
  • you question is quite confusing, you can do all the things you mentioned above so what is the problem exactly and is this question an opinion or about a problem you are facing. ? – Umair Apr 27 '18 at 07:04
  • I am editing my question to make it more clear. – Khemraj Sharma Apr 27 '18 at 07:06
  • @Umair edited my question. – Khemraj Sharma Apr 27 '18 at 07:11
  • @Khemraj i believe there is no option for bold font yet. So what you can do is take a normal font and give font ="bold-font.ttf" so that your font will be normal but bold. Another way is to use textStyle everywhere which is a time consuming task. And the third option is to use styles for declaring your fonts. and then use those styles in your views. – Umair Apr 27 '18 at 07:36
  • I am currently doing styling, Thanks for effort @Umair – Khemraj Sharma Apr 27 '18 at 08:15

3 Answers3

6

For your 2nd question:

You can add a custom bold font in a font-family, by defining a font with fontWeight="700":

<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
    <font
        android:fontStyle="normal"
        android:fontWeight="700"
        android:font="@font/montserrat_semi_bold" />
</font-family>

If a TextView uses this font-family with textStyle="bold", it will show up in montserrat_semi_bold

Josselin
  • 2,593
  • 2
  • 22
  • 35
1

There is a way you can do this by applying property called android:textStyle in your layout.xml you can do this:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold" />
Dumbo
  • 1,630
  • 18
  • 33
0

Setting font bold programatically this way

mTextView.setTypeface(Typeface.ttf, Typeface.BOLD);

Or

mTextView.setTypeface(mTextView.getTypeface(), Typeface.BOLD);

or You can use paint object and set it to text

Amjad Khan
  • 1,309
  • 15
  • 32
  • Do you really think this is convenient way than styling? Also i can achieve this by just using textStyle="bold" or italic dude. Thanks for suggestion. – Khemraj Sharma Apr 27 '18 at 06:48
  • Why this is convenient way because you may change your theme or you may customize it or change font so it can be fesible – Amjad Khan Apr 27 '18 at 06:58
  • You should please read my question not only title dude. Your answer does not meet my requirement . I want use custom bold font in font_family.xml – Khemraj Sharma Apr 27 '18 at 07:05