Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Is it possible to define a font in a theme?

Open saket opened this issue 7 years ago • 4 comments

Hello, I was trying to define a font in a theme, but it doesn't seem to work. Here's the theme:

<style name="BatmanIpsumTheme">
    <item name="fontPath">fonts/PragmataPro.ttf</item>
</style>

and XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/batman_ipsum_text"
    android:theme="@style/BatmanIpsumTheme" />

Update: Found this log message:

W: Can't create asset from res/drawable/seek_thumb.xml. Make sure you have passed in the correct path and file name.
   java.lang.RuntimeException: Font asset not found res/drawable/seek_thumb.xml
       at android.graphics.Typeface.createFromAsset(Typeface.java:190)
       at uk.co.chrisjenx.calligraphy.TypefaceUtils.load(TypefaceUtils.java:35)
       at uk.co.chrisjenx.calligraphy.CalligraphyUtils.applyFontToTextView(CalligraphyUtils.java:114)
       at uk.co.chrisjenx.calligraphy.CalligraphyUtils.applyFontToTextView(CalligraphyUtils.java:143)
       at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onViewCreatedInternal(CalligraphyFactory.java:146)
       at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onViewCreated(CalligraphyFactory.java:114)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:279)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
       at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
       at is.uncommon.layoutinflationtalk.MainActivity.onCreate(MainActivity.java:14)
       at android.app.Activity.performCreate(Activity.java:6237)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
       at android.app.ActivityThread.-wrap11(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Update 2: Looks like pullFontPathFromStyle() isn't working as expected? It's returning the wrong value for fontFromAttribute in line number 191. This is wrong because the value isn't even present in the View's styles. The library expects the value to be null so that it can check in the theme, right?

Update 3: I'm testing this on a Marshmallow (Android 6.0) emulator.

saket avatar Aug 09 '16 13:08 saket

Never tried, I use styles. But if your theme extended a base theme then it should work.

chrisjenx avatar Aug 15 '16 17:08 chrisjenx

Ah, so is this a corner case?

saket avatar Aug 16 '16 04:08 saket

Well you can set the fontPath in a theme. But never tried applying a theme directly to a View, I do this with the Toolbar and it works correctly. I would check that this works without Calligraphy.

chrisjenx avatar Aug 17 '16 16:08 chrisjenx

@Saketme what you're doing is not a theme, it's a style.

Try this:

    <style name="BatmanIpsumTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textAppearance">@style/BatmanIpsumTextAppearance</item>
    </style>
    <style name="BatmanIpsumTextAppearance" parent="TextAppearance.AppCompat">
        <item name="fontPath">fonts/PragmataPro.ttf</item>
    </style>

and use BatmanIpsumTheme on your activity/app as the theme.

@chrisjenx if it works, this could be added to README.md as well. It's more global solution than the current textViewStyle there.

TWiStErRob avatar Apr 13 '17 15:04 TWiStErRob