Exploring Android O: Fonts in XML



Google just released a few information about the next version of Android, which arrives later this year and is known for now as Android O.
With the first developer preview, we saw some exciting stuff that will have a big impact for developers and the apps they can make. New ways to use custom fonts and icons, a better way to deliver professional-level audio and awesome ways to connect with others for things like head-to-head gaming or local social applications.

One of the features of this announcement includes Fonts in XML — in this article we’re going to take a look at exactly what this and how we can make use of them within our applications!

Get started with Android O SDK


Android O Developer Preview, a program that gives you everything you need to make your apps compatible and build for the next version of Android.

Install Android Studio 2.4

To access the O Developer Preview, you must first update to Android Studio 2.4. Only Android Studio 2.4 includes support for all the new developer features available with Android O. So you need to get the canary version of Android Studio 2.4 to begin using the Android O SDK.

Install SDK and tools

Launch Android Studio 2.4 and open the SDK Manager by clicking Tools > Android > SDK Manager.
In the SDK Platforms tab, check Show Package Details. Below Android O Preview check the Android SDK Platform O.
Switch to the SDK Tools tab and check all items that have updates available. This should include the following that are required:
  • Android SDK Build-Tools 26.0.0 (rc1 or higher)
  • Android SDK Platform-Tools 26.0.0 (rc1 or higher)
  • Android Emulator 26.0.0
  • Support Repository
Click OK to install all the selected SDK packages.

Setup Device running Android O

Download the Android O system image for the Android Emulator. It's listed in the SDK Manager under Android O Preview as Google APIs Intel x86 Atom System Image.

Working With Fonts

Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources. This means, there is no need to bundle fonts as assets. Fonts are compiled in R file and are automatically available in the system as a resource.

Creating New Project

We’ll start by creating new project in Android Studio and do the basic setup required.Create a new project in Android Studio from File > New Project and fill the project details. While creating the project, I have selected the Empty Activity as default activity.

Update Build Configuration

Open build.gradle located under app module and Update compileSdkVersion,  buildToolsVersion, targetSdkVersion, and the Support Library version with the following versions:

Add Fonts As Resources

You can access the font resources with the help of a new resource type, font.To add fonts as resources do the following steps:
  1. Right-click the res folder and go to New > Android resource directory.
  2. Give Directory name: font
  3. In the Resource type list, select font, and then click OK.
Screen Shot 2017-03-29 at 9.31.41 PM-min.png
Screen Shot 2017-03-29 at 9.28.02 PM-min.png
Next step is to add fonts in the font folder. Google Fonts are beautiful web fonts that can be used without restrictions. You can download and install Google Fonts for free.
Before adding font files to font folder rename files according to the naming conventions:
  1. Resource name should contains be a-z,0-9,_
  2. Resource name should start with a-z,_
Below is the project structure after adding the fonts to the font resource folder:
Screen Shot 2017-03-29 at 10.24.27 PM-min.png
Double-click a font file to preview the file's fonts in the editor.
Screen Shot 2017-03-29 at 10.41.29 PM-min.png
Using fonts in XML layouts
To attach fonts to the TextView or in styles, use the fontFamily attribute.
Open activity_main.xml file and add a TextView to the layout. Then use the fontFamily attribute to access the font file.

Another method to set the font is to use the Properties window. Selected the Design tab at the bottom of the window and open the Properties window.
Screen Shot 2017-03-30 at 9.53.28 PM.png
The Android Studio layout preview, shown in the rightmost pane allows you to preview the font set in the TextView.
Screen Shot 2017-03-29 at 11.01.08 PM-min.png

Using fonts programmatically

You can retrieve fonts by using the getFont(int) method, where you need to pass the resource identifier of the font that you want to retrieve. This method returns a Typeface object.


Comments

Post a Comment

Popular posts from this blog

How to Use Kotlin in Your Android Projects

Reactive Programming with RxBinding

Android Debug Database: A Library for Debugging Android Databases and Shared Preferences

Kotlin: Do more with less code

Exploring Android O: Autosizing TextViews

Introduction to Spring Animation

Introduction to Android Bottom Navigation View

Getting started with Android Fingerprint Authentication