I applied a custom font to a TextView
, but it doesn\'t seems to change the typeface.
Here is my code:
Typeface myTypeface = Typeface
Make sure to paste the above code into onCreate() after your call to the super and the call to setContentView(). This small detail kept my hung up for awhile.
Well, after seven years you can change whole app textView
or what you want easily by using android.support
libraries 26++.
E.g:
Create your font package app/src/res/font and move your font into it.
And in your app theme just add it as a fontFamily:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
. . . ...
<item name="android:fontFamily">@font/demo</item>
</style>
Example for use with textView
only:
<style name="fontTextView" parent="@android:style/Widget.TextView">
<item name="android:fontFamily">monospace</item>
</style>
And add into your main theme:
<item name="android:textViewStyle">@style/fontTextView</item>
Currently it's worked on 8.1 until 4.1 API Jelly Bean And that's a wide range.
If you want to load the font from the network or easily style it, you can use:
https://github.com/shellum/fontView
Example:
<!--Layout-->
<com.finalhack.fontview.FontView
android:id="@+id/someFontIcon"
android:layout_width="80dp"
android:layout_height="80dp" />
//Java:
fontView.setupFont("http://blah.com/myfont.ttf", true, character, FontView.ImageType.CIRCLE);
fontView.addForegroundColor(Color.RED);
fontView.addBackgroundColor(Color.WHITE);
TextView txt = (TextView) findViewById(R.id.txt_act_spalsh_welcome);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Aramis Italic.ttf");
txt.setTypeface(font);
name of the font must be correct and have fun
After trying most of the solutions described in this thread, I accidentally found Calligraphy (https://github.com/chrisjenx/Calligraphy) - a library by Christopher Jenkins that lets you easily add custom fonts to your app. The advantages of his lib comparing to approaches suggested here are:
With Android 8.0 using Custom Fonts in Application became easy with downloadable fonts
.
We can add fonts directly to the res/font/ folder
in the project folder, and in doing so, the fonts become automatically available in Android Studio.
Now set fontFamily
attribute to list of fonts or click on more and select font of your choice. This will add tools:fontFamily="@font/your_font_file"
line to your TextView.
This will Automatically generate few files.
1. In values folder it will create fonts_certs.xml
.
2. In Manifest it will add this lines:
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
3.
preloaded_fonts.xml
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/open_sans_regular</item>
<item>@font/open_sans_semibold</item>
</array>
</resources>