2

I have created nested preferenceScreens.I want to add custom font to preferenceScreen title and summary. I try to use font which loaded to typeface. How can i do this? thanks.

here is my preference.xml :

   <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory android:title="@string/manage_device_title" 
         android:key="@string/device_category">
             <PreferenceScreen android:title="@string/manage_device"
                 android:key="@string/manage_device_KEY"
                 android:summary="@string/device_summary" >     
             </PreferenceScreen>
        </PreferenceCategory>
   </PreferenceScreen>
Namrata
  • 1,683
  • 1
  • 17
  • 28
Kelum Deshapriya
  • 258
  • 4
  • 14

1 Answers1

6

You need to create CustomPreference and CustomPreferenceCategory for that. Include that CustomPreference and CustomPreferenceCategory in your preference.xml

CustomPreference:

public class CustomPreference extends Preference {
Typeface fontStyle;

public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

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

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

@Override
protected void onBindView(View view) {
    super.onBindView(view);

    fontStyle = Typeface.createFromAsset(CA.getApplication().getApplicationContext().getAssets(), AppConstants.fontStyle);
    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    titleView.setTypeface(fontStyle);   
    titleView.setTextColor(Color.RED);
    TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
    summaryView.setTypeface(fontStyle); 
    summaryView.setTextColor(Color.RED);
    }
}

CustomPreferenceCategory:

 public class CustomPreferenceCategory extends PreferenceCategory {
Typeface fontStyle;

public CustomPreferenceCategory(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);

}

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

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

@Override
protected void onBindView(View view) {
    super.onBindView(view);

    fontStyle = Typeface.createFromAsset(CA.getApplication()
            .getApplicationContext().getAssets(), AppConstants.fontStyle);
    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    titleView.setTypeface(fontStyle);
    // titleView.setTextColor(Color.RED);
        }
}

In your Preference.xml you need to create PreferenceCategory and Preference with these custom classes.

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CustomPreferenceCategory android:title="@string/manage_device_title" 
     android:key="@string/device_category">
         <CustomPreference android:title="@string/manage_device"
             android:key="@string/manage_device_KEY"
             android:summary="@string/device_summary" >     
         </CustomPreference>
    </CustomPreferenceCategory>
</PreferenceScreen>

Note: Please use proper package name of CustomPerenceCategory and CustomPreference when refereing in preference.xml and for fontStyle add as per the need.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • thanks for answer. sorry to inform that i have add preferences to child preferenceScreen using preferenceScreen.addPreference() function in my code. when i use CustomPreference this method unavailable. so i can't add preference to child screen. so what should i do? – Kelum Deshapriya Feb 27 '14 at 09:32
  • @KelumDeshapriya : Ain't you using `addPreferencesFromResource(R.xml.preference)`? – Vikalp Patel Feb 27 '14 at 09:37
  • yeh. i use that. but i also use addPreference() to add my dynamic preference list to child preferenceScreen in programme. – Kelum Deshapriya Feb 27 '14 at 10:51
  • i have small problem. when i used device title it show different string rather than the one should use. look like for category title the font is change than set one – Kelum Deshapriya Feb 28 '14 at 05:55
  • @Vikalp Patel, How do i create for custom view and set typeface for `SwitchPreference` and `ListPreference`? – Hiren Dabhi Feb 01 '17 at 12:40