Reputation: 2248
I've been fooling around with custom fonts in Android. I know how to import a font and have a text view set to that font. I've noticed that once you have many textViews, this can become rather tedious.
Is there anyway to set an entire layouts font type to a certain font face?
Thanks in advance.
Upvotes: 0
Views: 944
Reputation: 617
You can use this method, Just pass top level viewgroups id and font typeface to this method and this will set font to your entire layout.
public void setFont(ViewGroup group, Typeface lTypeface)
{
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++)
{
v = group.getChildAt(i);
if (v instanceof TextView)
{
((TextView) v).setTypeface(lTypeface);
} else if (v instanceof EditText)
{
((EditText) v).setTypeface(lTypeface);
} else if (v instanceof Button)
{
((Button) v).setTypeface(lTypeface);
} else if (v instanceof TextInputLayout)
{
((TextInputLayout) v).setTypeface(lTypeface);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, lTypeface);
}
}
Upvotes: 0
Reputation: 1313
What I did was declare my own sub-class of TextView where I set the typeface in the constructor like this:
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
setTypeFace();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeFace();
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeFace();
}
public void setTypeFace()
{
this.setTypeface(StaticUtils.getDefaultFontNormal(getContext()));
}
}
Then in my layouts, if I use the fully qualified name, it works:
<ca.mycompany.mobile.ui.support.MyTextView
android:id="@+id/title_summaryreports"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:text="@string/title_strategies"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:textColor="#ff0000" />
Upvotes: 1