Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Font on Android

I want to use a custom font in an Android app but using custom fonts seems to be a pain?! I can setup a custom font programmatically by adding it to each TextView -> lots of boilerplate code. I can create a new class CustomTextView extends TextView and add setTypeface in the constructor -> no xml context aware help for attributes anymore.

Is there another way to setup a custom font? By XML, by style, by theme?

Thanks in advance!

P.S. Is there a possibility to set an underline by XML?

like image 561
GabrielWeis Avatar asked Apr 17 '11 14:04

GabrielWeis


People also ask

How do I install custom fonts on my Samsung?

From Settings, search for and select Font size and style. Then, tap Font size and style again. Tap Font style, and then tap + Download fonts. The Galaxy Store will automatically launch; tap the Install icon next to your desired font.


2 Answers

use this code for change font textview..

TextView newfont;
newfont=(TextView) findViewById(R.id.textView1);
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/DejaVuSerif-Italic.ttf");
newfont.setTypeface(font);
newfont.setText("This is the new font Text");

download DejaVuSerif-Italic.ttf file from web and put in fonts folder in assets folder.

like image 60
Kundan Chaudhary Avatar answered Sep 29 '22 16:09

Kundan Chaudhary


You can include custom fonts under assets/fonts and then using the code from

https://github.com/browep/AndroidCustomFontWidgets/

you can specify the font in your XML, e.g.

<com.github.browep.customfonts.view.FontableTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is in a custom font"
    app:font="MyFont-Bold.otf" />

The code supports FontableTextView and FontableButton, but it's quite easy to extend to support other widget types if you require.

like image 40
Jonathan Caryl Avatar answered Sep 29 '22 16:09

Jonathan Caryl