Reputation: 77
like the title says, I need to use a new font. I currently use Android Studio 2.3.3.
I have a file .ttf (a font file) and I would like to add this font in the res directory (/res/font).
I saw other questions, but it seems that things are different because a lot of people use Android Studio 3.0.
So, for us poor people, which is the right process?
Upvotes: 2
Views: 2432
Reputation: 8237
1.create a new fonts
directory in the assets
directory and put the your.ttf
font file here.
2.You can change to this .
TextView tv = (TextView) findViewById(R.id.tv);
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(), "fonts/your.ttf");
tv.setTypeface(tf);
Edit
You also can use android:typeface="sans"
、android:typeface="serif"
、android:typeface="monospace"
in your xml code .
Use android:fontFamily
in the xml code
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)
Also use in the style
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>
Upvotes: 1
Reputation: 238
you can use CustomTextView
public class CustomTextView extends TextView {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_font);
try {
if (fontName != null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(myTypeface);
}
} catch (Exception e) {
e.printStackTrace();
}
a.recycle();
}
}
}
You need to create a attrs in values folder
<resources>
<declare-styleable name="CustomTextView">
<attr name="font" format="string" />
</declare-styleable>
after for textview in xml
<com.packagename.view.CustomTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:textColor="#313131"
android:textAllCaps="true"
android:text="Title"
android:textSize="12sp"
app:font="@string/monte_semibold"
/>
the font name you mention in xml should be available in assets/fonts/ folder
Upvotes: 1