How do i get the default themed font when its overridden in android?

Viewed 65

I'm creating a custom view in android that needs to get the fontFamily when its overridden in a custom app theme like so:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:fontFamily">@font/raleway_semibold</item>
</style>

I want to have a way of getting this value, whilst also defaulting to the system default fontFamily if this custom override wasn't there.

I know I could provide a styleable attribute on the custom view but i wanna make use of the app theme so it stays consistent throughout the app.

Hope that makes sense Thanks!

1 Answers

I managed to solve this by doing this:

final TypedArray typedArray = theme.obtainStyledAttributes(new int[] {
    R.attr.fontFamily
});
Typeface font = typedArray.getFont(0);
if(font == null) 
    font = Typeface.DEFAULT;

and changing the xml value slightly to 'fontFamily':

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="fontFamily">@font/raleway_semibold</item>
</style>
Related