How to programmatically set the font size and style of an Android TextView

Android FAQ: How do I programmatically set the font size (and/or font style) for an Android TextView?

Solution: Setting the Android TextView font size programmatically is a simple two-step process. First, define everything about the font that you want to use in a resources file. For example, I put this code in a file named res/values/styles.xml:

<resources>

    <style name="fontForNotificationLandingPage">
        <item name="android:textStyle">italic</item>
        <item name="android:fontFamily">sans-serif-light</item>
        <item name="android:textColor">#333333</item>
        <item name="android:textSize">32sp</item>
        <item name="android:padding">2dip</item>
    </style>

</resources>

As you can see, the name I’ve assigned to this style is fontForNotificationLandingPage, and that XML shows how to set several font properties, including font style, the font family, font color, size, etc.

Next, in my Android fragment you just need to get a reference to your TextView and then call the setTextAppearance method on it, referencing your style, like this:

textView.setTextAppearance(getActivity(), R.style.fontForNotificationLandingPage);

Assuming that you know how to get a reference to the TextView whose font you want to modify programmatically, this is a simple process.