Skip to main content

Add custom font in Android using Calligraphy library

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’
}

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

Popular posts from this blog

Get Phone Number from Contact List - Android Tutorial

When you create an application to send sms or an application to make calls, getting a destination number from the contacts list is a common task. In this Android tip, I am going to show the code to fetch a number from the contacts list. Now let me tell you how to achieve the goal. First, you need to create an Intent object for the PICK_ACTION action. To open the contacts list, the table that contains the contacts information must be specified as a parameter of the constructor of the Intent class. You can refer to the table using ContactsContract.Contacts.CONTENT_URI. Then call the startActivityForResult () method passing the Intent object and request code to open the contacts list. After a contact is selected from the contacts list, to get the result, you need to override the onActivityResult(int reqCode, int resultCode, Intent data) method of the activity. You can call the getData() method of the data parameter to get the table or uri that contains the selected contact. From the t

Spinner with Search on DropDown - Android Tutorial

If you have more values on Dropdown of Spinner its hard to select the last item by making a long scroll. To overcome this issue Android introduced a component called  AutoCompleteTextView Yes it is!!! Then why Spinner with Search? There may be some requirement even though gave much knowledge about it. There is a simple and good library that helps us to achieve this -  SearchableSpinner Gradle dependencies {     ...     implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' } Usage Now replace your Normal Android Spinner on XML with the following < com.toptoche.searchablespinnerlibrary.SearchableSpinner     android:id="@+id/id_city"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@android:color/transparent"     android:padding="5dp" /> ___________________________________________________________

Set Focus on Spinner when select Item on Vertical Scroll - Android Tutorial

We may face an issue on Spinner lies on long vertical scroll, (i.e.) when selected and item from dropdown the focus moves to top of scroll. To avoid this please follow this piece of code spinner.setFocusableInTouchMode( true ); spinner.setOnFocusChangeListener( new View.OnFocusChangeListener() {     @Override     public void onFocusChange(View v, boolean hasFocus) {         if (hasFocus) {             if (spinner.getWindowToken() != null ) {                 spinner.performClick();             }         }     } });   _______________________________________________________________________________ Happy Coding...