11

I want to apply some font say Times New Roman to only my application. Not to the whole system and not to specific view. As far I know

to apply font to specific view we store font file in asset folder and get into the application as follow.

1]  Typeface  mFace = Typeface.createFromAsset(getContext().getAssets(),
                                     "fonts/samplefont.ttf");
    textView.setTypeface(mFace);


2] To apply font to whole application I can replace the DroidSans.ttf file with my font file.

I can use first way to apply font to my application but It won't be a good solution because I need to modify everywhere and I don't want to do that if there is any better way is available.

Vivek
  • 4,170
  • 6
  • 36
  • 50
  • Possible Duplicate of http://stackoverflow.com/questions/4395309/android-want-to-set-custom-fonts-for-whole-application-not-runtime – Abdul Rahman Feb 08 '13 at 02:07

3 Answers3

2

I made a custom textview widget and in the constructors made a call to this code:

public static void SetCustomFont (TextView t, String fontName, Context c) {
        Typeface tf = Typeface.createFromAsset(c.getAssets(),
                fontName);
        t.setTypeface(tf);
}

I'm using the same font over the whole application so I put the fontName in the constuctor and then did a global Find/Replace for TextView

chedabob
  • 5,835
  • 2
  • 24
  • 44
  • 1
    See http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android/7197867#7197867 for a full coded example of this. – Intrications Aug 26 '11 at 10:59
  • This doesn't answer the question. You need to inherit all the textviews/buttons/... within the app - including libraries :( – AlikElzin-kilaka Aug 01 '13 at 19:38
0

Android does not provide much in the way of support for applying fonts across the whole app (see this issue). You have 4 options to set the font for the entire app:

  • Option1: Apply reflection to change the system font
  • Option2: Create and subclass custom View classes for each View that needs a custom font
  • Option3: Implement a View Crawler which traverses the view hierarchy for the current screen
  • Option4: Use a 3rd party library.

Details of these options can be found here.

Community
  • 1
  • 1
Phileo99
  • 5,581
  • 2
  • 46
  • 54
-1

Check this to define fonts styles. In addition, you can refer to this style from your manifest file at the app level

<application
    android:theme="@style/Theme1">
Rajath
  • 11,787
  • 7
  • 48
  • 62
  • 1
    I want to use font other that "monospace, Sans, Serif etc." For that I need to include fontname.ttf file into asset folder and need to refer it. How should refer file which is in asset folder. – Vivek Mar 31 '11 at 12:44
  • Have a look at [Custom fonts and XML layouts (Android)](http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android) – Rajath Mar 31 '11 at 12:58
  • Neither of those examples are clear. I'm also looking to use an application wide font from a ttf format – Kevin Parker Apr 24 '11 at 19:07
  • Why is this as accepted answer? Does not help at all – PerracoLabs Mar 14 '14 at 21:01
  • This does not answer the question or even provide a hint to solve the question asked. – Mushtaq Jameel Mar 21 '14 at 05:20