So I\'d like to change the android:fontFamily
in Android but I don\'t see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don\'
The valid value of android:fontFamily is defined in /system/etc/system_fonts.xml(4.x) or /system/etc/fonts.xml(5.x). But Device Manufacturer might modify it, so the actual font used by setting fontFamily value depends on the above-mentioned file of the specified device.
In AOSP, the Arial font is valid but must be defined using "arial" not "Arial", for example android:fontFamily="arial". Have a qucik look at Kitkat's system_fonts.xml
sans-serif
arial
helvetica
tahoma
verdana
Roboto-Regular.ttf
Roboto-Bold.ttf
Roboto-Italic.ttf
Roboto-BoldItalic.ttf
//////////////////////////////////////////////////////////////////////////
There are three relevant xml-attributes for defining a "font" in layout--android:fontFamily, android:typeface and android:textStyle. The combination of "fontFamily" and "textStyle" or "typeface" and "textStyle" can be used to change the appearance of font in text, so does used alone. Code snippet in TextView.java like this:
private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
Typeface tf = null;
if (familyName != null) {
tf = Typeface.create(familyName, styleIndex);
if (tf != null) {
setTypeface(tf);
return;
}
}
switch (typefaceIndex) {
case SANS:
tf = Typeface.SANS_SERIF;
break;
case SERIF:
tf = Typeface.SERIF;
break;
case MONOSPACE:
tf = Typeface.MONOSPACE;
break;
}
setTypeface(tf, styleIndex);
}
public void setTypeface(Typeface tf, int style) {
if (style > 0) {
if (tf == null) {
tf = Typeface.defaultFromStyle(style);
} else {
tf = Typeface.create(tf, style);
}
setTypeface(tf);
// now compute what (if any) algorithmic styling is needed
int typefaceStyle = tf != null ? tf.getStyle() : 0;
int need = style & ~typefaceStyle;
mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
} else {
mTextPaint.setFakeBoldText(false);
mTextPaint.setTextSkewX(0);
setTypeface(tf);
}
}
From the code We can see: