3

I've noticed that the SwitchCompat's font does not seem to change with what I've set in the fontFamily field. I've also tried using styles with custom fontFamily (which works on TextViews) and even the switchTextAppearance field. It does apply on the preview (I know the preview is not really accurate) but not when I tried running it on my test device.

Here's my SwitchCompat:

<android.support.v7.widget.SwitchCompat
            style="@style/MyStyle.Body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/my_font_family"
            android:text="Enable"
            app:switchTextAppearance="@style/MyStyle.Body"/>

and here's my style:

<style name="MyStyle.Body" parent="Base.TextAppearance.AppCompat.Body1">
    <item name="android:textSize">14sp</item>
    <item name="android:fontFamily">@font/my_font_family</item>
    <item name="android:textColor">@color/color_primary_text</item>
</style>

As you can see, I only really want to change it's font


EDIT

I've changed my style to this

<style name="MyStyle.Switch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/color_primary_text</item>
    <item name="android:fontFamily">@font/my_font_family</item>
    <item name="fontFamily">@font/my_font_family</item>
</style>

still doesn't work though

Gama the Great
  • 225
  • 4
  • 16

5 Answers5

1

XML settings is not working, therefore I use following code:

someSwitch.setTypeface(ResourcesCompat.getFont(context, R.font.roboto));
Michalsx
  • 3,446
  • 5
  • 33
  • 46
0

Try

parent="Base.TextAppearance.AppCompat.Body1"

replace into this

parent="TextAppearance.AppCompat.Widget.Switch"
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
0

Try below

public class CustomSwitchCompact extends SwitchCompat {

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

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

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

    private void init() {
        if (!isInEditMode()) {
            Typeface myFonts = Typeface.createFromAsset(getContext().getAssets(),
                    "fonts/Roboto_Bold.ttf");
            setTypeface(myFonts);
        }
    }
}

XML file

<com.test.CustomSwitchCompact
        android:id="@+id/switch_compat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:checked="false"
        android:padding="20dp"
        android:text="SwitchCompat"
        android:textOff="OFF"
        android:textOn="ON"
        app:showText="true" />

Another way to achieve SwitchCompact with custom font

  <android.support.v7.widget.SwitchCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/adamSwitch"
        android:textColor="@color/top_color"
        android:textAppearance="@color/top_color"
        android:gravity="center"
        app:showText="true"
        android:fontFamily="@font/my_font_family"
        app:theme="@style/Custom.Widget.SwitchCompat"
        app:switchPadding="5dp"
        />

in style.xml

<style name="Custom.Widget.SwitchCompat" parent="Widget.AppCompat.CompoundButton.Switch" >
            <item name="android:textColorPrimary">@color/blue</item>  
            <item name="android:textSize">14sp</item>
            <item name="android:fontFamily">@font/my_font_family</item>
             <item name="android:textColor">@color/color_primary_text</item>
      </style>
Sabyasachi
  • 3,499
  • 2
  • 14
  • 21
  • the custom class will work (with some minor changes to use the new font family xml, **ResourceCompat.getFont()**), I already have something like this but this is my last option as of the moment... the custom style via _app:theme_ does not work, unfortunately. – Gama the Great Apr 16 '18 at 07:07
  • I see this problem as well using com.android.support:appcompat-v7:27.1.1. I am applying a style that includes a font family as well as making the text all caps. I see that the capitalization part of the style has been applied correctly to the SwitchCompat, but not the font. – vonWippersnap Aug 02 '18 at 02:24
0

The only thing that works for me is setSwitchTypeface, not setTypeface:

    my_switch.setSwitchTypeface(ResourcesCompat.getFont(context, R.font.my_font))
Gumby The Green
  • 603
  • 7
  • 10
0

I've used android:textAppearance instead of android:switchTextAppearance and custom font works now. Also my switch style have Widget.AppCompat.CompoundButton.Switch as parent

Yaroslav Shulyak
  • 151
  • 1
  • 12