3

I want to force Android's default font (Roboto in ICS and something else in older ones) to all the TextViews in my android app.

It should not change when user changes it from the System preferences.

Is that even possible?

I tried to set the typeface to normal in the application's theme. But that did not work. But when I tried monospace it worked.

I do not want to include any font files in my package. I've seen GMail app doing it in ICS. How are they doing it?

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
  • 2
    I have found that there are some inconsistencies across different devices when dealing with fonts. My guess is that if you wish to make this work accross all devices you are going to have to include copies of the font files inside your package (in assets) – FoamyGuy Jun 27 '12 at 15:32

3 Answers3

2

Yoy can extend TextView Object, and override constructor to force font type:

    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf"); 
    this.setTypeface(type);
jzafrilla
  • 1,416
  • 3
  • 18
  • 41
2

The solution provided by @Stefano Ortisi is absolutely wrong. The only way you can have CUSTOM FONTS in your application is by placing it in the assets directory and access it from there:

Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/CUSTOMFONT.tff");
TextView tv = (TextView) findViewById(R.id.TEXTVIEW_ID);
tv.setTypeface(tf); 

Following are the links to justify my claim:

Using a custom typeface in Android

There was a similar post related to this issue. Check this link before even trying for the solution given in the above post:

Custom typeface error

Community
  • 1
  • 1
Arun George
  • 18,352
  • 4
  • 28
  • 28
1

Just use the theme in your application. You can set it from AndroidManifest.xml

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" android:theme="@style/YourCustomTheme">

Then you have to create your custom theme in the style.xml file where

<style name="YourCustomTheme" >
     <item name="android:typeface">yourcustomfont</item>
</style>
Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41
  • 1
    @Stefano Ortisi have you ever implemented the solution you have provided? – Arun George Jun 27 '12 at 15:53
  • @ArunGeorge sure. You can check this little example project: https://dl.dropbox.com/u/6102128/FontTest.zip – Stefano Ortisi Jun 27 '12 at 16:00
  • Checked the project and as I said CUSTOM FONT can only be used programmatically. The font you used in the project's `style.xml` `serif` is one of the built-in typefaces. So you are not using a custom font but just an android build-in typeface. So you are absolutely wrong when you say `yourcustomfont` which could infact be `AndroidBuildInFonts` – Arun George Jun 27 '12 at 16:07
  • 2
    I want to force the default font. I do not want to include the ttf file. By default font, I mean the one which is already installed on the device. – Sudarshan Bhat Jun 28 '12 at 09:53