Android 8.0 (API level 26) introduces fonts in XML, a feature that lets you use fonts as resources. You can add the <TextView
0 file and are automatically available in Android Studio. You can access the font resources using the font resource type. For example, to access a font resource, use <TextView
2, or <TextView
3.To use the fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use Support Library 26.0. For more information on using the Support Library, refer to the section. To add fonts as resources, perform the following steps in Android Studio:
Create a font familyA font family is a set of font files along with style and weight details. In Android, you can create a new font family as an XML resource and access it as a single unit, instead of referencing each style and weight as separate resources. By doing this, you let the system select the correct font based on the text style you are using. To create a font family, perform the following steps in Android Studio:
Use fonts in XML layoutsUse your fonts, either a single font file or a font from a font family, in <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>1 objects or in styles by using the <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>2 attribute. Note: When you use a font family, the <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>3 switches on its own, as needed, to use the font files from that family. Add fonts to a TextViewTo set a font for a <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>3, do one of the following:
The Android Studio layout preview, shown in the rightmost pane in Figure 5, lets you preview the font set in the <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>3. Figure 5. Previewing fonts in layout preview. Add fonts to a styleOpen the <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>8 file and set the <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>2 attribute to the font file you want to access.
Use fonts programmaticallyTo retrieve fonts programmatically, call the val typeface = resources.getFont(R.font.myfont) textView.typeface = typeface 0 method and provide the resource identifier of the font you want to retrieve. This method returns a val typeface = resources.getFont(R.font.myfont) textView.typeface = typeface 1 object. Although the system picks the best style for you from the fonts' information, you can use the val typeface = resources.getFont(R.font.myfont) textView.typeface = typeface 2 method to set the typeface with specific styles. Note: The <style name="customfontstyle" parent="@android:style/TextAppearance.Small"> </style>3 does this for you. Kotlinval typeface = resources.getFont(R.font.myfont) textView.typeface = typeface JavaTypeface typeface = getResources().getFont(R.font.myfont); textView.setTypeface(typeface); Use the Support LibraryThe Support Library 26.0 supports fonts in XML on devices running Android 4.1 (API level 16) and higher. Note: When you declare font families in XML layout through the Support Library, use the app namespace to ensure that your fonts load. <?xml version="1.0" encoding="utf-8"?> <font-family xmlns:app="http://schemas.android.com/apk/res-auto"> </font-family>To retrieve fonts programmatically, call the val typeface = resources.getFont(R.font.myfont) textView.typeface = typeface 4 method and provide an instance of val typeface = resources.getFont(R.font.myfont) textView.typeface = typeface 5 and the resource identifier. Kotlinval typeface = ResourcesCompat.getFont(context, R.font.myfont) JavaTypeface typeface = ResourcesCompat.getFont(context, R.font.myfont); Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates. Last updated 2022-12-13 UTC. [{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }] |