Using font in XML in API <26

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 the use of fonts as a resource in res? [font in XML] If so, which API is supported?

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

+5
source share
4 answers

According to Google, it is supported by Android 4.0+ (API 14+) if 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 Change History

I was hoping that I could use this in application widgets in Android versions earlier than 8 (API 26), however this is not possible since AppCompatTextView cannot be used in application widgets. None of the third-party Android O 'Fonts in XML' alternatives, such as the library

+13
source

Here is an example: min sdk support 18 (in the app)

https://github.com/mukundrd/androidFonts

+2
source

Some users will land on this page to find instructions on using custom fonts in your xml application. Here is how I did it:

The font resource directory is missing by default. You can create it manually by following the documentation here [too much work]

Or use the font selector from the GUI -

  • Select the "TextView in Design" tab.

  • Open the fontFamily xml attribute drop-down list.

  • Scroll down to "More Fonts".

  • Select “Downloadable Font” from the popup that appears.

Android Studio will automatically create a font resource directory with the necessary declarations mentioned in the above documentation. After this step, you can copy your own font file (for example myfont.ttf) to the font directory and install the 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 the application, you can set fontFamily to 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> 

[fontFamily prefix with android: if above doesn't work]

Screenshots:

Screenshot 1

Screenshot 2

Note. This answer assumes you are using Android Studio 3+ and supporting the 26+ library version

+2
source

For API below 26, to view, you need to use app:fontFamily="@font/custom_font_from_resource" instead of android:font

work with lib 26 + support

0
source

Source: https://habr.com/ru/post/1268783/


All Articles