Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwitchCompat fontfamily won't change

I'm using Android Studio and have a SwitchCompat widget in my main activity. The default fontfamily it had was sans-serif-medium and I changed it to quicksand_light. I also have some TextViews with each of their fontfamily's set to quicksand_light. On the design tab of the xml file for my activity it shows the SwitchCompat having the quicksand_light fontfamily just as the TextViews, but when I run it on my phone or on an emulator the SwitchCompat's fontfamily is sans-serif-medium. Is there something extra I need to do to change the fontfamily or is this a bug or is this just me?

like image 566
K.R. Avatar asked Jan 07 '18 18:01

K.R.


1 Answers

I haven't dived deep into why it's not working correctly when defining the fontFamily attribute in xml, but IT WORKS if you set the typeface programmatically.

Here's an example using data-binding.

Add the following data-binding adapter:

@BindingAdapter("labelTypeface")
fun setLabelTypeface(view: SwitchCompat, @FontRes id: Int) {
    view.typeface = ResourcesCompat.getFont(view.context, id)
}

and use it in your layout:

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:labelTypeface="@{R.font.stratum}"
    ... />
like image 66
Tudor Luca Avatar answered Oct 23 '22 13:10

Tudor Luca