Elte Hupkes
Elte Hupkes

Reputation: 2978

Why does `fontFamily` work on `TextView` but not on `Switch`?

I am trying to use XML fonts in Android. I have two views, one TextView:

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Blaaah"
  android:textSize="16sp"
  app:fontFamily="@font/my_custom_font" />

And one Switch:

<Switch
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="16sp"
  android:text="Blaaah"
  app:fontFamily="@font/my_custom_font"/>

Put underneath each other, this is what they look like:

TextView and Switch side by side

The TextView picks up the font, the Switch does not. Switch inherits from TextView, so I'm at a bit of a loss why this doesn't work. If I do switch.setTypeface(...) from code, the font is picked up just fine. Does anybody have an idea what is going on, or how I might be able to set the Switch's font from XML?

Upvotes: 0

Views: 70

Answers (2)

Nehal Godhasara
Nehal Godhasara

Reputation: 795

This is working for me,

<androidx.appcompat.widget.SwitchCompat
            android:id="@+id/switch1"
            style="@style/tvStyle_poppinsMedium_Small_Black"
            android:text="@string/push_notifications"                
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />



  <style name="tvStyle_poppinsMedium_Small_Black">
        <item name="android:fontFamily">@font/poppins_medium</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">@dimen/text_small</item>
    </style>

Hope this will help!!

Upvotes: 1

mrtcnkryln
mrtcnkryln

Reputation: 324

You can change switch component programmatically

switchObj.setSwitchTypeface(Typeface.createFromAsset(mContext.getAssets(), "custom_font.ttf"));

Upvotes: 0

Related Questions