Showing posts with label Font for Android. Show all posts
Showing posts with label Font for Android. Show all posts

Thursday, December 5, 2019

Change Font for Android Project

Starting from Android-Studio 3.0 it's 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 whichever you want and paste it inside font folder. The structure should be something like below
Here
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 the font in layout using
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/dancing_script"
app:fontFamily="@font/dancing_script"/>
To change Programmatically
 Typeface typeface = getResources().getFont(R.font.myfont);
   //or to support all versions use
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
 textView.setTypeface(typeface);  
To change the 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 font style and font-weight to the font resource which will be used to render that specific variant. Valid values for font style are normal or italic, and font-weight conforms to the CSS font-weight specification
1. To change font-family in a 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 use
Typeface typeface = ResourcesCompat.getFont(context, R.font.lobster);
 textView.setTypeface(typeface);  
To change the 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 For more info

That is all. If any help related to this post please comment.

Thank you, guys. 

Enjoy coding.