1

Is it possible to set a custom font in an Android application?

I tried what is posted here, but I don't know where my extends Application class is...

Any help?

EDIT:

I tried the following:

  • Add an assets folder and insert the font inside as seen here:

enter image description here

  • Add a new class that extends from Application

  • Call this new class from my AndroidManifest.xml.

  • I went to my style and added it.

MyApp.java:

public class MyApp extends Application {
  @Override
  public void onCreate() {
     super.onCreate();
    FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf");
    //  This FontsOverride comes from the example I posted above
  }
  }

AndroidManifest.xml:

<application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:name=".MyApp"
      android:theme="@style/AppTheme">
     ....

styles.xml:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:fontFamily">default</item>
 </style>

But my font is still not changning... any idea?

Then the MyApp class is called. But no effect on my fonts...

EDIT2: I realized that my buttons apply the custom font after I set a custom style for my buttons. Here is my custom button style:

<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
    <item name="textAllCaps">false</item>
    <item name="android:textAllCaps">false</item>
</style>

And here is how it looks now:

enter image description here

So: my button is applying the style, but not the TextView. Any idea on why my custom font is not being applied for all items in application?

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131

3 Answers3

1

there is a grate library for custom fonts in android:custom fonts

here is a sample how to use it.

in gradle you need to put this line

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

and then make a class that extends application an write this code `public class App extends Application { @Override public void onCreate() { super.onCreate();

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                    .setDefaultFontPath("your font path")
                    .setFontAttrId(R.attr.fontPath)
                    .build()
    );
}

}`

and in the activity class put this method before onCreate.

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));

and the last thing in your manifest file write like this.

 <application
    android:name=".App"

and it will change the whole activity to your font! its simple and clean!

Shia G
  • 1,444
  • 11
  • 10
  • I'm not looking for an android font... Acutally I have to use the "Raleway" font... – Sonhja Dec 02 '15 at 17:09
  • its not a font its to change your fonts and you can add any font you want and it will appear every where if you want i can post the code how i used it – Shia G Dec 02 '15 at 17:12
  • Oh, that's a custom library! :O I have to give it a try! – Sonhja Dec 02 '15 at 17:23
1

Try this code. But you need to initialize your TextView or Button in code by findViewByIdMethod(int id).

TextView yourTextView = findViewById(R.id.YourTextViewId);    
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),"raleway_regular.ttf");
yourTextView.setTypeface(font);
mtwain
  • 101
  • 5
  • I know there are ways to do it one by one... and this works fine one by one. But I would like to make it generally... – Sonhja Dec 02 '15 at 17:08
0

If you want to set font for entire application, just follow this approach https://stackoverflow.com/a/8854794/3718505 .

But it works only for static content.

Community
  • 1
  • 1
mtwain
  • 101
  • 5