I want to set the font for my application. The font is like "JOURNAL". But the problem is, i don't know how to integrate it in to myapplication. and if i integrate it then would it be for all the application or for only the selected application? because I want it to be set just for only one application. not for all. So for that what should i have to do ? I have seen here. but i think it is for only TextView and not for whole application font. So, Is there anything i have to do in manifest file ??? or what else i have to do ?? Please help me regarding it.
Asked
Active
Viewed 679 times
2 Answers
2
I agree to @Kheldar statement. There is no method to change the font in an Android app. Try this code to avoid calling a set method each time you want to change the font of an element.
public class MyTextView extends TextView {
Context context;
String ttfName;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
for (int i = 0; i < attrs.getAttributeCount(); i++) {
this.ttfName = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.package.my", "ttf_name");
init();
}
}
private void init() {
Typeface font = Typeface.createFromAsset(context.getAssets(), ttfName);
setTypeface(font);
}
@Override
public void setTypeface(Typeface tf) {
super.setTypeface(tf);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:package="http://schemas.android.com/apk/res/com.package.my"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
<com.package.my.MyTextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/icon"
package:ttf_name="My-font.otf" />
</RelativeLayout>

nahwarang
- 654
- 5
- 9
-
Thanks for the Reply. But I have that font file in to my pc. named as "JOURNAL.ttf" Sowhere should i have to put that font file ?? – Shreyash Mahajan Aug 30 '11 at 04:26
-
Put it in the assets folder. name it with lower cases and change the xml file to match the file name. – nahwarang Aug 30 '11 at 07:10
-
suppose i have button in layout and on the button text i want to set this font then ?? Please help me regarding this. – Shreyash Mahajan Aug 30 '11 at 07:23
-
Just change the model's extension from TextView to Button. – nahwarang Aug 30 '11 at 07:38
-
Ok. Thanks its works great. I have also find one example from here. http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html – Shreyash Mahajan Aug 30 '11 at 08:49
1
You cannot modify the fonts system-wide from an application (at least, as far as I know and barring some exploit).
Study this link: http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/

Kheldar
- 5,361
- 3
- 34
- 63
-
-
There is little help i want. If i want to set the font in xml file then ? because in tutorial the demo is for to set the font in java file not in xml file. – Shreyash Mahajan Aug 30 '11 at 04:57
-
what should i have to do if every time i dont want to use that two lines to set the font ??? – Shreyash Mahajan Aug 30 '11 at 05:22
-
You should look into theming then. Themes are precisely for this purpose! – Kheldar Aug 30 '11 at 07:37