Download Font Style For Android Studio

Aug 15, 2019  For instance, nearly all Samsung Android devices let you change the font style. Samsung has pre-installed a few extra fonts besides the default one, but. Sep 04, 2017 Custom Fonts on Android with Android Support Library. Android Studio flags the app. Another usage of the font family is via a style (or text appearance). We could specify the fontFamily attribute to use the font family created in step ii.

In our last post, we've looked at an easy way to apply a custom font to all TextViews, without writing a single line of additional code for each instance. If you experimented with our guide, you might have noticed that some TextView parameters, like textSize, continue to work very well. In contrast, textStyle, which defines if it's a regular, bold or italic font style, does not work anymore. This post will show a way to integrate the textStyle feature into our custom font TextView.

Hint: since this blog post series got more extended along the way, we decided to add a Github repository with the entire code base and an example activity.

Custom Fonts on Android Series Overview

  1. Using Font Styles

Adding Font Files

The first thing to do is adding the font files (preferably as .ttf file) to the project. You'll need one .ttf file for each style. Continuing our example from last week with Source Sans Pro, your assets folder now should look like this:

Download Font Style For Android Studio Windows 10

The base for applying text styles to our custom font is done. Next, we'll look how to integrate the textStyle property.

Using the XML textStyle-Property

Download Font Style For Android Studio Windows 7

You've seen in the previous blog post how to setup a custom view for use in the XML layout files. In this blog post, we changed the package name from a code snippet of our eat foody project to an open source class in our tutorial repository. If you worked with Android before, nothing of the following code snippet should surprise you:

The first five XML properties are standard and work without any customization. The last textStyle works on normal TextViews, but not if you apply a custom font. However, we prefer to continue to use the standard android:textStyle instead of a special customfont:textStyle or similar. There is a bit of code necessary, which we'll show in the next section.

Side note: if you are interested in the option of using customfont:textStyle, we'll use that method in the next blog post to change the font via XML.

Implementation of CustomFontTextView

We've the font files and know that we can continue to use the textStyle property. Lastly, we've to adjust our CustomFontTextView class to understand the textStyle parameter and apply to the correct font style.

The first challenge is to get the information, which was defined in textStyle. This happens with one line of code:

attr are the attributes which are applied to the TextView with the second constructor parameter. We can use that object to get the XML property information with the getAttributeIntValue() function.

Let's take a look at the parameter. ANDROID_SCHEMA is the constant with the same value as you define it in the top level of your XML (xmlns:android='http://schemas.android.com/apk/res/android'). The second parameter is the property name; in our case textStyle. The last parameter defines the default value, if the property was not set.

Change font android studio

We chose Typeface.NORMAL since that is the most appropriate value when taking a look at the used constants in the TextView class:

ConstantValue
normal0
bold1
italic2
bolditalic3

If there is no font style defined, we default to the regular font style.

Now we learned what values the int textStyle can be set to. Let's take a look at a function, which takes this into consideration.

You'll see the difference to the code in our previous blog post. We now don't just apply a font, we load it depending on the textStyle parameter.

Download Font Style For Android Studio

The entire class would now look like this:

This should be everything you need to use the standard textStyle to apply different font styles to your custom fonts.

Reviewing the Results

Let's look at an example layout with the standard Roboto font and multiple custom fonts (in different styles):

The result is just as expected:

Next Steps

In this blog post you've seen how to use the textStyle to apply different styles to your custom font. All you need is one CustomFontTextView implementation and everything else is straightforward in XML. In our next blog post in this series, we'll go one step further. We'll show you how you can even set the font via XML.

If you have questions, let us know in the comments or on twitter @futurestud_io!

Exploring Downloadable Fonts using Support Library & Android Studio

I recently had the pleasure of using the newly supported Downloadable Fonts feature that was introduced with Android Oreo, when changing Winnie’s font from Roboto to Lato.

Along with Fonts in XML, Support Library 26 introduced support for Downloadable Fonts, with backwards compatibility till API 14. Historically, using custom fonts have usually required the use of third-party libraries, so it was nice to see more support from the Android framework itself.

What is Downloadable Fonts, and why use it?

Downloadable Fonts allow the ability for apps to request fonts from a font provider application instead of including font files in the apk or downloading it themselves. A font provider application retrieves fonts and caches them locally so that other apps can request and share fonts. How cool is that!

Google Play Services (on devices with version 11+) is one such font provider, which I used for Lato as it is a Google Font. There isn’t documentation yet on how to implement your own font provider if you were to use a custom font not available through Google Play Services, however you can use fonts in XML which is still an improvement over what it used to be!

As you can see in the image above, apps using Downloadable Fonts make a FontRequest using the FontsContract API which retrieves the Typeface from the Font Provider. The Font Provider does not need to download fonts if it already exists in the Font cache.

Developers don’t need to use these APIs directly if using Downloadable Fonts through XML, which is described more in the next section.

Advantages of using Downloadable Fonts:

  • Reduced APK size — which can significantly impact your app installs!
  • Since the apps can share fonts from the same provider, this results in less usage of precious memory, disk space and cellular data. Can you guess how many Roboto font files are in your phone’s storage right now? 🙀 (yes there are apps that have them in their APKs).

Great, let’s get started!

There are three ways of implementing Downloadable Fonts in your app — using Android Studio & Google Play services, programmatically or via the Support Library. I used Android Studio to generate the required files, and used the Fonts in XML feature from the Support library to apply the downloaded fonts.

I decided to do it via XML because then you can declare the required fonts in your app’s manifest file, which allows the framework to download them ahead of time. If doing it programmatically, you can only request for fonts after the app is launched, which can cause a delay in the first layout time. Also, it is less work do it via XML!

1. If you want to use Android Studio to generate the required files, then you’ll need version 3.0+. Add the following (version 26+) to your module’s build.gradle:

2. Select a text view in your app that you want to apply the font to and click on the fontFamily attribute under Attributes in the graphical layout.

Select the “More Fonts…” at the bottom, which will open the dialog below.

3. Make sure to have “Create downloadable font” selected. This results in three files being downloaded — lato.xml, font_certs.xml and preloaded_fonts.xml.

lato.xml
This file contains the font attributes for loading a Typeface from the Google Fonts Provider Application.

font_certs.xml
The system uses these certificates to verify the font provider’s identity, to avoid getting fonts from an unknown source. If using the steps above, Android Studio should have automatically generated the string certificates for dev and prod in font_certs.xml below.

Android Studio Font Size

preloaded-fonts.xml
This file is referenced in the Android manifest which helps the framework pre-load fonts to avoid delays when the app is launched.

Download Font Style For Android Studio 2017

4. Make sure this line is added to your app’s Manifest file, Android Studio should have done this automatically:

5. Great, now you are ready to apply the fonts in XML!

All I had to do was set the font family in the app’s theme to get TextViews throughout the app to change to Lato, including parts that were bold or italicized. However, if you want to configure the weights, you can follow the same steps to get Lato Bold using Android Studio, and change the weight manually in lato_bold.xml that you can then apply in XML layouts:

The whole thing felt like magic! But….

Gotchas

When using Support Library 26, I noticed that the toolbars throughout the app was still in Roboto, resulting in a hideous Roboto-Lato combination! Even explicitly setting the fontFamily of the toolbar in XML didn’t work. This is because Toolbar does not implement the fontFamily property, like a TextView does.

Including the fontFamily in an XML style and setting that to be the toolbar’s app:titleTextAppearance worked — the toolbar titles were now in Lato! Good news is that this seems to be fixed in Support Library 27, so this workaround shouldn’t be needed.

Android Studio Change Font Style

There were a couple of other places where Lato did not get applied — one where I was dynamically creating textviews with bold styling, and the bottom navigation view for which I used a third-party library. I initially thought that this would do the trick:

I used the ResourcesCompat.getFont() method to load the Typeface and set it on both views, and voilà, there was no more Roboto! However, at one point I saw a ResourcesNotFound exception when setting the Typeface for the BottomNavigation using this method.

Essentially, if the font hasn’t been loaded by the time this method is called or the user doesn’t have the required version of Google Play Services, it will throw this exception. The safest way to avoid this is to load the font programmatically, if you are unable to set the style through XML. Here is the official documentation on how to do that.

There is also this crashwithFontsContractCompat in 27.0.2, it has been fixed for a future release of support library and doesn’t seem to be an issue in 26.1.0

Other requirements and limitations worth noting:

  • Your compileSDK should be at least SDK 26, and if you want to use Android Studio to generate the font files, it should be version 3.0+. If programmatically or using Fonts in XML to apply Downloadable Fonts, you must use at least Support Library 26.
  • Using Google Play Servicesas a font provider works on devices that have version 11 and above, otherwise it uses the default system font.
  • As mentioned earlier, you may not be able to use Downloadable Fonts if you’re using a custom font that isn’t available through Google Play Services. You might have to write your own custom FontProvider in this case which doesn’t have documentation yet.

Download Font Style For Android Studio Software

I (surprisingly) enjoyed transitioning our app from Roboto to Lato thanks to Fonts in XML & Downloadable Fonts, hope you enjoy these features too! I’d love to hear from you in the comments or on Twitter, and don’t forget to 👏 the post :).