-2

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ext1 Dev
  • 33
  • 2
  • 6

3 Answers3

0

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);
            }
        }
    }

}

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
0

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)

Lorenzo Vincenzi
  • 1,153
  • 1
  • 9
  • 26
0

Use assets folder in res to use different types of fonts in your application. Add ttf files in assets and continue..

venu
  • 13
  • 1
  • 6