Monday, October 10, 2011

[android-developers] how to change the font typeface in PreferenceScreen

I have font in file *.ttf in Assets folder. I want to set this font
typeface for TextViewews. I have this code, but it doesn't change
anything:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
setContentView(R.layout.preferences);

PreferenceScreen prefScreen = getPreferenceScreen();
ListAdapter listAdapter = prefScreen.getRootAdapter();

for (int i = 0; i < listAdapter.getCount(); i++) {
CheckBoxPreference cbPrefs = (CheckBoxPreference)
listAdapter.getItem(i);

LinearLayout linearLayout = (LinearLayout) cbPrefs.getView(null,
null);
RelativeLayout relativeLayout = (RelativeLayout)
linearLayout.findViewById(R.id.title_layout);
TextView title = (TextView)
relativeLayout.findViewById(android.R.id.title);
Typeface lucidaGrande =
Typeface.createFromAsset(getApplicationContext().getAssets(), "Lucida
Grande.ttf");
title.setTypeface(lucidaGrande);
}
}

In another Activity - ListActivity I have overrided
SimpleCursorAdapter and in its bindView(View view, Context context,
Cursor cursor) method I have used something like that:

@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
LinearLayout parentItemLayout = (LinearLayout) view;
LinearLayout childItemLayout = (LinearLayout)
parentItemLayout.findViewById(R.id.child_layout);
LinearLayout infoItemLayout = (LinearLayout)
childItemLayout.findViewById(R.id.info_layout);
TextView title = (TextView) infoItemLayout.findViewById(R.id.title);

Typeface lucidaGrande =
Typeface.createFromAsset(getApplicationContext().getAssets(), "Lucida
Grande.ttf");
title.setTypeface(lucidaGrande, Typeface.BOLD);
}

And it works properly. I have my own layout for list item, so I can
use findViewById. In Preference Screen I have only CheckBoxPreference
also with my own layout. So I should also can use findViewById method
to get acces to TextViewes.

How can I do similar thing in PreferenceActivity?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment