Search code examples
androidandroid-fonts

Font-family usage in android


  • To use fonts in android now , we directly use android:fontFamily="@font/yourFont" in android and we can apply the font on a view directly.

  • If we have different versions of same font such as bold,italic,normal we can create a font-family as::

font/lobster.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />

    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />

    <font
        android:fontStyle="bold"
        android:fontWeight="600"
        android:font="@font/lobster_bold" />
</font-family>
  • Now my question is that how can I directly use the italic version of font, not directly with android:fontFamily="@font/lobster_italic" but with lobster.xml reference.

Solution

  • In your xml use

    android:fontFamiliy="@font/lobster"
    android:textStyle="italic"
    

    Or in code

    textview.setTypeface(textview.getTypeface(), Typeface.ITALIC);