1

Hello I want to set same font family to entire Android application. I have seen this Is it possible to set a custom font for entire of application? but this all doing using code. I want to set in Theme such that it will work in all. It should convert the font family of ActionBar text also.

Any way to do this ?

styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>
Community
  • 1
  • 1
Mick
  • 1,179
  • 5
  • 14
  • 24
  • You have the answer here: http://stackoverflow.com/questions/16404820/how-to-set-default-font-family-for-entire-android-app – Gil Fitussi Apr 17 '14 at 14:09
  • @GilFitussi Yes but that is using `android:fontFamily` that require API level 16. I want to do from API level 8, Any way to do this ? – Mick Apr 17 '14 at 14:14

1 Answers1

1

If you want to set the TypeFace for all of your Views without having to do it programatically each time (and still work across all versions of Android), your best option would be to subclass the View and have it automatically set the TypeFace you want.

IE.

public class CustomTextView extends TextView{

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTextView(Context context) {
        super(context);
        init();
    }

    public void init(boolean bold) {
        setTypeface(Typeface.createFromAsset(getAssets(), "fonts/your_font_file.ttf"));
    }
}

If you want to really optimize that even further, you can use a static reference to that TypeFace and use that so you don't need to recreate the TypeFace every time the View loads.

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • No I want to do using `Theme` of the application – Mick Apr 17 '14 at 14:28
  • 1
    That's not possible across all versions of Android. You'd think something like that would be, but your only options are to either use your own subclass, manually set the TypeFace, or manually traverse every View and set the TypeFace if it's an instance of TextView (which is costly and I wouldn't recommend doing) – Cruceo Apr 17 '14 at 14:30
  • Okay that fine your approach but what about the text that appear in appcompat ActionBar how do I set custom font of them ?? – Mick Apr 17 '14 at 14:32
  • If you're using a CustomView for your ActionBar, you can use your subclassed-TextView directly in the layout's XML file and it would be done automatically when it's loaded. – Cruceo Apr 17 '14 at 14:36
  • No I am not using CustomView for my ActionBar then how would you customize the font of ActionBar ? – Mick Apr 17 '14 at 14:37
  • I can't test this right this second so I apologize if it's not a viable solution, but you could try something like traversing the MenuItems currently in the ActionBar when you load it, then check if (menuItem.getActionView() instanceof TextView) ((TextView)menuItem.getActionView()).setTypeFace(your_typeface); Could also check if it's an instanceof Button, as well. Not sure which off the top of my head. – Cruceo Apr 17 '14 at 14:42
  • +1 Nice suggestion. I will accept your answer if nobody post any good approach over this – Mick Apr 17 '14 at 14:46