Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using font in XML in API < 26

Tags:

android

fonts

https://developer.android.com/preview/features/working-with-fonts.html

Android API 26 supports this but do we have a support library that can provide use of fonts as a resource in res? [ font in XML ] If yes, up-to what API is it supported?

I am talking about the Android O SDK feature which allows us to set font in XML as given in the link above. This is not a native feature in MM I m sure.

like image 913
sziraqui Avatar asked Jun 12 '17 17:06

sziraqui


People also ask

How do I add fonts to Android keyboard?

GO Launcher Long press on the home screen and select GO Settings > Font > Select Font. Pick the font you want or tap Scan to add files stored on your device.


2 Answers

According to Google it is supported on Android 4.0+ (API 14+) as long as these conditions are met:

  • You are using the support library version 26.0.0-beta1 or later
  • You are using AppCompat

Sources:

Fonts in XML - Using the support library

Support Library revision history

I was hoping I could use this in app widgets on Android versions earlier than 8 (API 26), however it's not possible, as AppCompatTextView cannot be used in app widgets. None of the third party alternatives to Android O's 'Fonts in XML' such as the Caligraphy library work in app widgets either.

The only alternative for app widgets is using an ImageView instead of a TextView and using RemoteViews methods such as setImageViewUri(...) and setImageViewBitmap(...), both of which are problematic.

like image 21
ra3o.ra3 Avatar answered Oct 11 '22 18:10

ra3o.ra3


To support font family on API lower than 26, we need to use

<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/> 

instead of

<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"/> 
like image 142
Yuri Misyac Avatar answered Oct 11 '22 19:10

Yuri Misyac