So I\'d like to change the android:fontFamily
in Android but I don\'t see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don\'
From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
in combination with
android:textStyle="normal|bold|italic"
this 16 variants are possible:
fonts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>
I think I am too late but maybe this solution helpful for others. For using custom font place your font file in your font directory.
textView.setTypeface(ResourcesCompat.getFont(this, R.font.lato));
If you want to use a TextView in so many places with same font family, extend the TextView class and set your font like this:-
public class ProximaNovaTextView extends TextView {
public ProximaNovaTextView(Context context) {
super(context);
applyCustomFont(context);
}
public ProximaNovaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public ProximaNovaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
Typeface customFont = FontCache.getTypeface("proximanova_regular.otf", context);
setTypeface(customFont);
}
}
And then use this custom class in xml for the TextView like this:-
<com.myapp.customview.ProximaNovaTextView
android:id="@+id/feed_list_item_name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
This is the way to set the font programmatically:
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
tv.setTypeface(face);
put the font file in your assets folder. In my case I created a subdirectory called fonts.
EDIT: If you wonder where is your assets folder see this question
Kotlin Code - Textview to set custom font from Resource Folder
Set custom font from res -> font -> avenir_next_regular.ttf
textView!!.typeface = ResourcesCompat.getFont(context!!, R.font.avenir_next_regular)
One simple way is by adding the desired font in the project.
Go to File->New->New Resource Directory Select font
This will create a new directory, font, in your resources.
Download your font (.ttf). I use https://fonts.google.com for the same
Add that to your fonts folder then use them in the XML or programmatically.
XML -
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/your_font"/>
Programatically -
Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
textView.setTypeface(typeface);