1

How can i change those font attributes of a SwitchPreference?

I already tried to use app:layout attribute with the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@android:id/title"
        style="@android:style/TextAppearance.Widget.TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="MY-FONT"
        android:text="Title" />

    <TextView
        android:id="@android:id/summary"
        style="@android:style/TextAppearance.Widget.TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="MY-FONT"
        android:text="Summary" />    
</LinearLayout>

That doesn't work well, because the switch was missing and everything looks a bit messy.

What is an easy (and working) way to customize the fontfamily and textSize?

Jan Möller
  • 301
  • 4
  • 19
  • The switch is missing because when you provide a layout like that, it's your responsibility to provide a Switch, a TextView and so on. Add a `` element with an id `@android:id/switch_widget`. And try to style your layout, try using `ConstraintLayout`. I personally faced the same issue and I styled the preference completely using a custom layout. – Suleyman Mar 14 '19 at 23:11
  • @Suleyman thanks - adding element was the solution! – Jan Möller Mar 16 '19 at 16:04
  • No worries! Good luck! – Suleyman Mar 16 '19 at 16:34

2 Answers2

0

You need to create a Custom Switch Preference by extending its main class

public class CustomSwitchPreference extends SwitchPreference {
    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

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

    public CustomSwitchPreference(Context context) {
        super(context);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        TextView title1= (TextView) holder.findViewById(android.R.id.title);
        title1.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
        TextView title2= (TextView) holder.findViewById(android.R.id.summary);
        title2.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
    }
}

and use this is in your preferences xml

from

<SwitchPreference 
   .....
/>

to

<com.example.myapp.CustomSwitchPreference
....
/>

This Worked for me

-1

To change the font family in android first you need to have the font in your app something like this Your font goes in the font directory under res

Now to use it, your going in the right way

 <Button
            ...

            android:fontFamily="@font/nameOfFont"
            ... />

Let me now if it works for you

950CA
  • 1
  • 3
  • Sorry, that doesn't help. I need to know how to set font for a SwitchPreference, which is part of the Preferences (PreferenceScreen). The SwitchPreference has no fontFamily attribute like a button or textview. – Jan Möller Mar 14 '19 at 21:54
  • Hi Jan, [Stackoverflow ](https://stackoverflow.com/questions/5334828/how-can-i-change-font-size-in-preferencescreen) in that link they were able to get what you want, i think – 950CA May 28 '19 at 12:42