Good, how do I use custom fonts in android studio for Layouts, can they only be set programmatically or is there a way to place them directly in Layout in XML?
Good, how do I use custom fonts in android studio for Layouts, can they only be set programmatically or is there a way to place them directly in Layout in XML?
You can programmatically use fonts in your assets
directory, you must create an object of type Typeface
from a source file.
You place your source file in the assets folder, you can place a subfolder inside that is called fonts if you want to take more order and instances a Typeface
object so
//Definicion de clase de utilidades Fuentes.java por ejemplo
public static Typeface myFont(Context context) {
return Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
}
Then you apply your source to the component you want
TextView tv = findViewById ....
tv.setTypeface(Fuentes.myFont(this));
The tedious thing about this is if you want to apply the font to many components in your app.
To apply directly to Layout
, as far as I know android-typeface-textview
works only for TextView
, plus it is deprecated
You can try Calligraphy as suggested, I have not tried it; but for your demo I see that it can be used directly to Layout
by means of an attribute fontPath
and in TextViews
, styles
and Buttons
, try it and tell us.
Some examples that I provide are the following:
<EditText
android:id="@+id/edit_text"
fontPath="fonts/Roboto-Bold.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:hint="@string/edit_text_hint"/>
<CheckBox
fontPath="fonts/Oswald-Stencbab.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="@string/checkbox_custom"/>
<Button
android:id="@+id/button_bold"
fontPath="fonts/Roboto-Bold.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp"
android:text="@string/button_defined"/>
<TextView
fontPath="fonts/Roboto-None.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/defined_incorrect"/>
Googling a bit ....
There is a library that you can use as you mention, here is :
This would be the custom TextView
you would use in this way:
<com.mobsandgeeks.ui.TypefaceTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stackoverflow en español"
geekui:customTypeface="fonts/custom_font.ttf" />