Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Custom Font android using lib

Tags:

java

android

xml

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

like image 253
userLearner Avatar asked Nov 03 '14 20:11

userLearner


People also ask

Which library supports fonts in Android?

Android O and AndroidX Library add support for Downloadable Fonts.


2 Answers

Remove the namespace from the TextView.

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

(Change app:fontPath to fontPath)

Annoyingly we can't resolve the auto-res namespace.

like image 95
Chris.Jenkins Avatar answered Oct 06 '22 00:10

Chris.Jenkins


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

like image 30
userLearner Avatar answered Oct 05 '22 22:10

userLearner