In my project I want to use Monsterrat font for my application ,In some screen I want to use Monsterrat italic ,Monsterat medium . I want to declare once and use them in whole application. I don't understand how can I do that.
-
You can define your font in xml like this inside your textview `fontPath="fonts/ralewayregular.ttf"` – Vivek Mishra Oct 04 '18 at 06:45
-
Check this - https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml – Siddharth jain Oct 04 '18 at 06:49
3 Answers
Use this Class
In your xml
<your.class.path.RubikMediumTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"/>
and java class is and change font in Typeface face= Typeface.createFromAsset(context.getAssets(), "font/rubik_medium.ttf");
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class RubikMediumTextView extends TextView {
Typeface font;
public RubikMediumTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public RubikMediumTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public RubikMediumTextView(Context context) {
super(context);
init(context);
}
public void init(Context context) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >=
android.os.Build.VERSION_CODES.KITKAT) {
if (!isInEditMode()) {
Typeface face= Typeface.createFromAsset(context.getAssets(), "font/rubik_medium.ttf");
setTypeface(face);
}
}else{
if (!isInEditMode()) {
Typeface face= Typeface.createFromAsset(context.getAssets(), "font/rubik_medium.ttf");
setTypeface(face);
}
}
}
}

- 1,888
- 2
- 9
- 20
First way:
Put your fonts into res/font directory and create two style. In this example I setted also the text color and text size:
<style name="FirstStyle" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:fontFamily">@font/md_grotesk_regular</item>
<item name="fontFamily">@font/md_grotesk_regular</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">@dimen/font_12</item>
</style>
Then declare the android:theme for each activity in you Manifest like this:
<activity
android:name=".SomeActivity"
android:theme="@style/FirstStyle" />
This will set the chosen style for all text defined in the activity.
Second way:
Look at @Sandeep Parish answer and create two custom textView, each with his own style, size, color, etc etc. Call the to customize each TextView so you can have two styles in the same layout (but this isn't a good practise except for some type of layouting)

- 1,153
- 1
- 9
- 26
Use assets folder in res to use different types of fonts in your application. Add ttf files in assets and continue..

- 13
- 1
- 6