Are you fed up of Custom Views to set fonts? Or traversing the ViewTree to find TextViews?
Sometime we want some other font for our Android application then you can add custom font in Android using Calligraphy library.
Dependency
Include the dependency Download (.aar)
dependencies {
compile ‘uk.co.chrisjenx:calligraphy:2.2.0’
}
Sometime we want some other font for our Android application then you can add custom font in Android using Calligraphy library.
Dependency
Include the dependency Download (.aar)
dependencies {
compile ‘uk.co.chrisjenx:calligraphy:2.2.0’
}
Add Fonts
Add your custom fonts to assets/. All font definitions are relative to this path.
On Assets you should right-click New Directory, call it "fonts". In the finder put the .ttf or .otf font files in there.
Create Class
Create a class that extends Application and 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()
);
}
}
Manifest.xml
Add the created class(above) in manifest.xml as below
<application
.
.
.
android:name=".App">
In All your Activity class :
In all your activity class put this method before onCreate
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
attrs.xml
<resources>
<attr format="string" name="fontPath"/>
<item name="calligraphy_tag_id" type="id"/>
</resources>
Now you are done your app with your favorite font.
NOTE : If you want different fonts for different activities
In your Calligraphy startup in your Application, add the line:
@Override public void onCreate() { super.onCreate(); CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/some-other-custom-font.ttf") .addCustomStyle(AppCompatTextView.class, android.R.attr.textViewStyle) .setFontAttrId(R.attr.fontPath) .build() ); //.... }
Now it is easy to make your app looks more cool using this cool library
____________________________________________________________________________________________
Happy Coding...
Comments
Post a Comment