setTypeface/android:fontFamily - Set the Typeface and Style in a TextView, in java

How to Set the Typeface and Style in a TextView, in java

setTypeface was added in API Level 1
Related XML Attribute: android:fontFamily

setTypeface (Typeface tf)
Sets the typeface and style in which the text should be displayed. Note that not all Typeface families actually have bold and italic variants, so you may need to use setTypeface(Typeface, int) to get the appearance that you actually want.

2. In the MainActivity.java file, add the following the to imports section.

       import android.graphics.Typeface; 

3.Add the code below to the onCreate method.

          myTextView1.setTypeface(Typeface.SERIF);

4. All the options are:

          myTextView.setTypeface(Typeface.SERIF); //The NORMAL style of the default serif typeface.


          myTextView.setTypeface(Typeface.MONOSPACE); //The NORMAL style of the default monospace typeface.

          myTextView.setTypeface(Typeface.SANS_SERIF); //The NORMAL style of the default sans serif typeface.

          myTextView.setTypeface(Typeface.DEFAULT_BOLD); //The default BOLD typeface object.

          myTextView.setTypeface(Typeface.DEFAULT); //The default NORMAL typeface object

5. Compile and run!

Related Articles: