How to create custom fonts in android studio

こ雲淡風輕ζ 提交于 2019-12-01 20:33:18

if you use android studio , you should create assets folder from right click at

app ---> New --->Folder --->Assets Folder

choose destination to your assets folder default main

create directory called fonts in assets then place your font

|assets

    |-----------------fonts

        |-------------------Roboto-Black.ttf

|java

|res

AndroidManifest.xml

finally use this code

Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString()); 
viewTotalValue.setTypeface(fontRobo );

You need to place your font file at the root of your project, inside a folder called 'assets'. You should end up with this structure :

src
|------------assets
    |-----------------fonts
        |-------------------font.ttf
|------------java
|------------libs
|------------res
AndroidManifest.xml

In android studio, the assets folder have to be placed under the "main" folder. Not under the "res" folder.

Since Android Studio uses the new Gradle-based build system, you should be putting the "assets/" folder inside of the source sets.

That was working for me:

src/main/assets/fonts/examalpefont.ttf

You have to download custom font .ttf file. In my case I downloaded din.ttf file. put that in app->assets->fonts->din.ttf Write the code in MainActivity.java file

    TextView tx = (TextView)findViewById(R.id.textView1);
    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/din.ttf");
    tx.setTypeface(custom_font);

//that's it . Done!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!