a. How to implement new fonts in Android file system?
b. How to change default font of android file system?
Assumption: you have idea of Android framework & file system build process.
How
to implement new fonts in system?
Step
1:
To
add
font
file
in
file
system.
Keep
your
“.ttf”
file
on
following
path.
...
/frameworks/base/data/fonts/fontFileName.ttf
Step
2:
update
Android.mk
(
.../frameworks/base/data/fonts/Android.mk)
file
to
include
your
“.ttf”
file
in
build.
copy_from
:=
\
...
DroidSansMono.ttf \fontFileName.ttf
...
DroidSansMono.ttf \fontFileName.ttf
step
3:
Introduce
font family in system.
Open
this file .../external/skia/src/ports/SkFontHost_android.cpp
search "gSansNames" text (font family)
static
const char* gSansNames[] = {
"monospace", "courier", "courier new", "monaco", NULL
};
include your font family name as below gSansNames .
"monospace", "courier", "courier new", "monaco", NULL
};
include your font family name as below gSansNames .
static const char *gYourFontFamilyNames[] = {
"your font name", NULL
};
then
search initialisation of “gSystemFonts”
and
include your font family name in systemFonts e.g
static
const FontInitRec gSystemFonts[] = {
{
"fontFileName.ttf",
gYourFontFamilyNames
},
{
"DroidSans.ttf",
gSansNames
},
{
"DroidSans-Bold.ttf",
NULL
},
{
"DroidSerif-Regular.ttf",
gSerifNames
},
...
}
step
4:
include
your font in Typeface class, so that you can access font from java files.
...
/frameworks/base/graphics/java/android/graphics/Typeface.java
as
android defined Typeface for system fonts
public
static final Typeface MONOSPACE;
add
your
custom
font
type
face
as.public
static
final
Typeface
CUSTOMFONT;
MONOSPACE = create("monospace", 0);
CUSTOMFONT=create("your font name", 0);
MONOSPACE = create("monospace", 0);
CUSTOMFONT=create("your font name", 0);
step 5:
update
attrs.xml file, to introduce your font in xml parser.
Open
files .../frameworks/base/core/res/res/values/attrs.xml
search
this
<!--
Default
text
typeface.
-->
<attr
name="typeface">
<enum
name="normal"
value="0"
/>
<enum
name="sans"
value="1"
/>
<enum
name="serif"
value="2"
/>
<enum
name="monospace"
value="3"
/>
<enum
name="customfont"
value="4"
/>
</attr>
&
add
<enum
name="customfont"
value="4"
/>.
step
6.
map
enum
value
of
“ customfont”
to
Typeface
class
value.
Open
file .../frameworks/base/core/java/android/widget/TextView.java
search
this
private
static
final
int
MONOSPACE
=
3;
&
add this
private
static
final
int
CUSTOMFONT=4;
search
this method and update
private
void
setTypefaceByIndex(int
typefaceIndex,
int
styleIndex)
{
Typeface
tf
=
null;
switch
(typefaceIndex)
{
case
SANS:
tf
=
Typeface.SANS_SERIF;
break;
case
SERIF:
tf
=
Typeface.SERIF;
break;
case
MONOSPACE:
tf
=
Typeface.MONOSPACE;
break;
case
CUSTOMFONT:
tf
=
Typeface.CUSTOMFONT;
break;
}
setTypeface(tf,
styleIndex);
}
Now
build your file system,
You
will able to use custom fonts from xml as well as from java.
e.g.
<TextView
android:typeface="customfont"
/>
android:typeface="customfont"
/>
&
TextView
tvTest = (TextView)
findViewById(R.id.textViewTest);
tvTest.setTypeface(Typeface.CUSTOMFONT );
tvTest.setTypeface(Typeface.CUSTOMFONT );
Note:
you will not be able to build your project from eclipse with above
snaps example.
For
that
either
you
have
to
update
your ADT or
build
from
other
ways,
because
eclipse
does
not
know "customfont" till that you have added in ADT.
How
to change default fonts of android?
To
change default fonts of android, you have to open
.../external/skia/src/ports/SkFontHost_android.cpp ( follow step 3 of above).
Search "#define DEFAULT_NAMES" , comment and add your default font
family
//#define
DEFAULT_NAMES gSansNames
#define
DEFAULT_NAMES
gYourFontFamilyNames
//
name
define
in
step
3.
Note:
your whole system default font will be CustomFont.
Thanks & regards
Imran Ali