Gama the Great
Gama the Great

Reputation: 235

How to change the font family of SwitchCompat?

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

Upvotes: 4

Views: 2796

Answers (5)

Yaroslav Shulyak
Yaroslav Shulyak

Reputation: 181

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

Upvotes: 0

Gumby The Green
Gumby The Green

Reputation: 631

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

    my_switch.setSwitchTypeface(ResourcesCompat.getFont(context, R.font.my_font))

Upvotes: 0

Michalsx
Michalsx

Reputation: 3631

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

someSwitch.setTypeface(ResourcesCompat.getFont(context, R.font.roboto));

Upvotes: 1

Sabyasachi
Sabyasachi

Reputation: 3599

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>

Upvotes: 0

Gowthaman M
Gowthaman M

Reputation: 8282

Try

parent="Base.TextAppearance.AppCompat.Body1"

replace into this

parent="TextAppearance.AppCompat.Widget.Switch"

Upvotes: 0

Related Questions