Reputation: 6092
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.
Upvotes: 47
Views: 38205
Reputation: 3676
This might be an undesired way of doing it, but if you have a global font (typeface) in xml, you could just create a Textview and get the typeface from there
TextView dummy = new TextView(context);
doSomethingWithTheTypeface(dummy.getTypeface());
Upvotes: 0
Reputation: 10038
To complete Yuri's answer, Fonts are supported back to API 16.
To create font family (where different weights of fonts are specified), you should create my_font_family.xml
under /res/font/ as written in the doc:
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont_regular"/>
<font app:fontStyle="normal" app:fontWeight="600" app:font="@font/myfont_semi_bold" />
<font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont_italic" />
</font-family>
To use this in TextView
xml, you should use app:fontFamily="@font/my_font_family"
, be careful with app:
instead of android:
.
To choose specific fontWeight
, you can use android:textFontWeight="200"
but it only works on API>=28. To choose different font based on weight prior to API 28, you have two options. You can either use android:textStyle="bold"
to specify only "normal/italic/bold", but not exact weight. Or you can directly use specific weight of font like this: app:fontFamily="@font/myfont_semi_bold"
.
Please Note: If you are using custom textview, YourTextView
, in your app, it has to extend AppCompatTextView
and not merely android.widget.TextView
Upvotes: 30
Reputation: 4910
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"/>
Upvotes: 69
Reputation: 6092
Some users will land up to this page searching for instructions to use custom fonts in your app from xml. Here's how I did it:
The font resource directory is not present by default. You can create it manually by following the documentation here [too much work]
Or use font selector from GUI-
Select a TextView in Design tab.
Open drop down of fontFamily
xml attribute.
Scroll down to "More Fonts".
Choose a 'Downloadable font" from the pop up that appears.
Android Studio will automatically create font resource directory with necessary declarations mentioned in documentation above. After this step you can copy your custom font file (say myfont.ttf) in font directory and set your desired font from xml for example:
<TextView
android:fontFamily="@font/myfont"
android:id="@+id/aboutus_head_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/who_are_we" />
If you want to use the same font throughout your app, you can set fontFamily your AppTheme in styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- custom font for the entire app -->
<item name="fontFamily">@font/myfont</item>
</style>
[ prefix fontFamily with android:
if above did not work ]
Screenshots:
Note: This answer assumes you are using Android Studio 3+ and support library version 26+
Upvotes: 7
Reputation: 852
According to Google it is supported on Android 4.0+ (API 14+) as long as these conditions are met:
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.
Upvotes: 37
Reputation: 106
Here is an example, min sdk support 18 (in app)
https://github.com/mukundrd/androidFonts
Upvotes: 2