user1534364 user1534364 - 2 months ago 7
Android Question

Adding Custom Font android using lib

I am using android custom font lib Calligraphy https://github.com/chrisjenx/Calligraphy.

But there is no effect on textview. I am using the below code:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

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


In XML:

<TextView
tools:context=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Touch Listner"
android:textSize="18sp"
app:fontPath="fonts/gotham-book.ttf" />


In attrs:

<resources>
<attr name="fontPath" format="string"/>
</resources>


In assets/font/gotham-book.ttf

Answer

Create the class that extends Application, now and the following code in it. and declare it in manifest under application tag.

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                    .setDefaultFontPath("fonts/Roboto-ThinItalic.ttf")
                    .setFontAttrId(R.attr.fontPath)
                    .build()
    );

Comments