Android - Using Custom Font

How to use custom font in a project written in Android Studio

Update 2021:

Create a folder named font inside the res folder and copy your font

Sample Image

All font names must be only: lowercase a-z, 0-9, or underscore.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/abc_font" />

For programmatic use:

textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))

For Android Studio 4.2+ there's even now a menu option:

Sample Image

Android - Using Custom Font

On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts

EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets folder not the res folder. So

assets/fonts

Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF but myfont.ttf this way must be in lower case

Android Studio 3.0 Custom font in styles.xml

Ok. I solve this issue - the reason of this behavior is very simple... I was extending Activity instead of AppCompatActivity

How to set custom font for a whole application in Android?

Write a class

public class MyApp extends Application{
// Put the onCreate code as you obtained from the post link you reffered
}

now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp

<application
android:name=".MyApp"
...
>
...
</application>

So whenever the App is opened , onCreate method of MyApp class would be invoked , and the font would be set.

Update
Put font file under assets/fonts/your_font_file.ttf

Put this line under onCreate method of your application class(MyApp)

TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/your_font_file.ttf");

Source File for TypefaceUtil

public class TypefaceUtil {

/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
*
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {

final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("serif", customFontTypeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
try {
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
}

Now update your style.xml file

put the below line your style which is included for your activity in manifest file

  <item name="android:typeface">serif</item>

Hope this helps

How to set custom font in .xml file instead of .java file?

For your reference,

 public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyTextView(Context context) {
super(context);
init();
}

public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/yourfont.ttf");
setTypeface(tf ,1);

}
}

In XML,

 <you_package.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Your text"
/>

Using a custom typeface in Android

Is there a way to do this from the
XML?

No, sorry. You can only specify the built-in typefaces through XML.

Is there a way to do it from code in
one place, to say that the whole
application and all the components
should use the custom typeface instead
of the default one?

Not that I am aware of.

There are a variety of options for these nowadays:

  • Font resources and backports in the Android SDK, if you are using appcompat

  • Third-party libraries for those not using appcompat, though not all will support defining the font in layout resources



Related Topics



Leave a reply



Submit