Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom fonts in bold styles

I have downloaded custom font to use on my app. I want to set style bold for that font. I have used the following code but its not working:

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);
like image 810
Dhanesh Avatar asked Jan 12 '12 08:01

Dhanesh


3 Answers

Neither of these answers worked for me.

Maybe they worked for others, but the way I got the bold version of a font working in my program, I did the following:

  1. Copy/pasted the font .ttc - in this case AmericanTypewriter.ttc - to a folder I created in my main/assets/ directory called /fonts. So, main/assets/fonts/AmericanTypewriter.ttc

  2. I made sure I had 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"/>
    
  3. At the top of my Activity, declared a TextView object:

    private TextView myTextView;
    
  4. In the same Activity's onCreate(), inserted the following code:

    Typeface americanFont = Typeface.createFromAsset(getAssets(),
            "fonts/AmericanTypewriter.ttc");
        Typeface americanFontBold = Typeface.create(americanFont, Typeface.BOLD);
        myTextView = (TextView) findViewById(R.id.myTextView);
        myTextView.setTypeface(americanFontBold);
    
like image 129
robkriegerflow Avatar answered Nov 09 '22 08:11

robkriegerflow


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

Here is correct one

Typeface bold = Typeface.create(tf, Typeface.BOLD);  
like image 37
Harshad Avatar answered Nov 09 '22 07:11

Harshad


Try this

tv.setTypeface(null, Typeface.BOLD);
like image 4
Vamshi Avatar answered Nov 09 '22 09:11

Vamshi