Coolest feature in Android Oreo for Developers
Aug 22, 2017 · 2 min read

So Google finally launched the Android 8.0 otherwise known as Android Oreo and it comes with some pretty cool features for users as well as developers. Being an Android developer the coolest feature i found in Android Oreo is the Fonts in XML.
What’s New and How is it different ?
Before Android Oreo the task of setting fonts was pretty intensive.
- Download the font
- Create a fonts folder in your assets directory and place your font files in it.
- Now to set font for each textview you need to create a Typeface and then set the typeface to each TextView
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");tv1.setTypeface(typeFace);
tv2.setTypeface(typeFace);
tv3.setTypeface(typeFace);
tv4.setTypeface(typeFace);
tv5.setTypeface(typeFace);
tv6.setTypeface(typeFace);
And we needed to do this for each textview in each Layout file .
But now thanks to Android Oreo this process has been made very simple and effective. All we need to do is
- In the res directory create a new Android resource directory named fonts
- Create a new xml file and define the font family inside it
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"
app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
<font android:fontStyle="italic" android:fontWeight="400" android:font="@font/myfont-Italic"
app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" /></font-family>
3. Use the font family directly where the textview is defined in the XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>There you go . This new feature is going to reduce the number of code lines by the hundreds.
