Sonhja
Sonhja

Reputation: 8448

Why my custom font is not being applied to all my application?

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:

enter image description here

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?

Upvotes: 1

Views: 997

Answers (3)

mtwain
mtwain

Reputation: 101

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.

Upvotes: 0

Shia G
Shia G

Reputation: 1464

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!

Upvotes: 1

mtwain
mtwain

Reputation: 101

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);

Upvotes: 1

Related Questions