How to set custom fonts in bold

I downloaded a custom font for use in my application. I want to set the style for a live font. I used the following code but did not work:

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BRADHITC.otf"); Typeface bold = Typeface.create(tf, Typeface.DEFAULT_BOLD); TextView tv = (TextView) findViewById(R.id.cou_text); tv.setTypeface(tf); 
+6
source share
4 answers

try it

 tv.setTypeface(null, Typeface.BOLD); 
+3
source

DEFAULT_BOLD is of type Typeface. Typeface.create () requires an int.

Here is the correct option

 Typeface bold = Typeface.create(tf, Typeface.BOLD); 
+8
source

None of these answers worked for me.

They may have worked for others, but as I got the bold version of the font working in my program, I did the following:

  • Copy / paste the .ttc font - in this case, AmericanTypewriter.ttc - into the folder that I created in the main / assets / directory called / fonts. So main / assets / fonts / AmericanTypewriter.ttc

  • I made sure that I have a TextView with an id in my xml:

     <TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is American Bold"/> 
  • At the top of my activity, a TextView object is declared:

     private TextView myTextView; 
  • The following code is inserted in the same Activity onCreate ():

     Typeface americanFont = Typeface.createFromAsset(getAssets(), "fonts/AmericanTypewriter.ttc"); Typeface americanFontBold = Typeface.create(americanFont, Typeface.BOLD); myTextView = (TextView) findViewById(R.id.myTextView); myTextView.setTypeface(americanFontBold); 
+1
source

Late answer, but perhaps useful:

 textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/BRADHITC.otf"), Typeface.BOLD); 

Thus, I changed the appearance of the TextView SlidingTabLayout .

+1
source

Source: https://habr.com/ru/post/905800/


All Articles