How to change fontFamily of TextView in Android
From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:
android:fontFamily="sans-serif" // roboto regularandroid:fontFamily="sans-serif-light" // roboto lightandroid:fontFamily="sans-serif-condensed" // roboto condensedandroid:fontFamily="sans-serif-black" // roboto blackandroid:fontFamily="sans-serif-thin" // roboto thin (android 4.2)android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
in combination with
android:textStyle="normal|bold|italic"
this 16 variants are possible:
- Roboto regular
- Roboto italic
- Roboto bold
- Roboto bold italic
- Roboto-Light
- Roboto-Light italic
- Roboto-Thin
- Roboto-Thin italic
- Roboto-Condensed
- Roboto-Condensed italic
- Roboto-Condensed bold
- Roboto-Condensed bold italic
- Roboto-Black
- Roboto-Black italic
- Roboto-Medium
- Roboto-Medium italic
fonts.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="font_family_light">sans-serif-light</string> <string name="font_family_medium">sans-serif-medium</string> <string name="font_family_regular">sans-serif</string> <string name="font_family_condensed">sans-serif-condensed</string> <string name="font_family_black">sans-serif-black</string> <string name="font_family_thin">sans-serif-thin</string></resources>
This is the way to set the font programmatically:
TextView tv = (TextView) findViewById(R.id.appname);Typeface face = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf");tv.setTypeface(face);
put the font file in your assets folder. In my case I created a subdirectory called fonts.
EDIT: If you wonder where is your assets folder see this question
Starting from Android-Studio 3.0 its very easy to change font family
Using support library 26, it will work on devices running Android API version 16 and higher
Create a folder font
under res
directory .Download the font which ever you want and paste it inside font
folder. The structure should be some thing like below
Note: As of Android Support Library 26.0, you must declare both sets of attributes ( android: and app: ) to ensure your fonts load on devices running Api 26 or lower.
Now you can change font in layout using
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:fontFamily="@font/dancing_script"app:fontFamily="@font/dancing_script"/>
To change Programatically
Typeface typeface = getResources().getFont(R.font.myfont); //or to support all versions useTypeface typeface = ResourcesCompat.getFont(context, R.font.myfont); textView.setTypeface(typeface);
To change font using styles.xml create a style
<style name="Regular"> <item name="android:fontFamily">@font/dancing_script</item> <item name="fontFamily">@font/dancing_script</item> <item name="android:textStyle">normal</item> </style>
and apply this style to TextView
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/Regular"/>
you can also Create your own font family
- Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
- Enter the file name, and then click OK. The new font resource XML opens in the editor.
Write your own font family here , for example
<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>
this is simply a mapping of a specific fontStyle and fontWeight to the font resource which will be used to render that specific variant. Valid values for fontStyle are normal or italic; and fontWeight conforms to the CSS font-weight specification
1. To change fontfamily in layout you can write
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/lobster"/>
2. To Change Programmatically
Typeface typeface = getResources().getFont(R.font.lobster); //or to support all versions useTypeface typeface = ResourcesCompat.getFont(context, R.font.lobster); textView.setTypeface(typeface);
To change font of entire App Add these two lines in AppTheme
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:fontFamily">@font/your_font</item> <item name="fontFamily">@font/your_font</item> </style>
See the Documentation , Android Custom Fonts Tutorial For more info