4

I have created folder named fonts in my asset folder and put a .ttf file in it. And Assigning that fontfamily to my text like this

txtName.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/segoeui.ttf"));

But I want to assign this font in xml file. May be something like this.

<TextView
        android:id="@+id/txtName"
        style="@style/My_Name"
        android:fontFamily="@android:asset/fonts/segoeui"
         />
Yawar
  • 1,924
  • 3
  • 29
  • 39

5 Answers5

3

To include ttf file in your app is lead to increase your app size.

Downloadable Font is best solution to use other fonts. Use following references:

document: https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html

sample: https://github.com/googlesamples/android-DownloadableFonts

and still you want to include fontFile then use following answer: https://stackoverflow.com/a/27588966/8240915

Sumit Jain
  • 1,100
  • 1
  • 14
  • 27
1

You can create a custom FontTextView : -Add this custom FontTextView in src package:

package com.example.android.ui;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class FontTextView extends TextView {

    private static Map<String, Typeface> mTypefaces;

    public FontTextView(final Context context) {
        this(context, null);
    }

    public FontTextView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        if (mTypefaces == null) {
            mTypefaces = new HashMap<String, Typeface>();
        }

        final TypedArray array = context.obtainStyledAttributes(attrs, styleable.FontTextView);
        if (array != null) {
            final String typefaceAssetPath = array.getString(
                    R.styleable.FontTextView_customTypeface);

            if (typefaceAssetPath != null) {
                Typeface typeface = null;

                if (mTypefaces.containsKey(typefaceAssetPath)) {
                    typeface = mTypefaces.get(typefaceAssetPath);
                } else {
                    AssetManager assets = context.getAssets();
                    typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                    mTypefaces.put(typefaceAssetPath, typeface);
                }

                setTypeface(typeface);
            }
            array.recycle();
        }
    }

}

-In res/values add the tt_attrs.xml

<resources>

    <declare-styleable name="FontTextView">
        <attr name="customTypeface" format="string" />
    </declare-styleable>

</resources>

-In your layout which you want add font textview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:geekui="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.example.android.ui.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="30sp"
        geekui:customTypeface="fonts/segoeui.ttf" />

    <com.entreprise.android.ui.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="@string/app_name"
        geekui:customTypeface="fonts/Times New Roman.ttf" />

    <com.entreprise.android.ui.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="@string/app_name"
        geekui:customTypeface="fonts/arial unicode ms.ttf" />

</LinearLayout>

all this source code is inspired from https://github.com/ragunathjawahar/android-typeface-textview

Sofien Rahmouni
  • 4,354
  • 1
  • 21
  • 22
0

Simply use this code. just simply add segoeui.ttf with the file name.

android:fontFamily="fonts/segoeui.ttf"
Adnan Amjad
  • 2,553
  • 1
  • 20
  • 29
  • Its working but I this is not working in style and demanding API level at least 16, but I cant go up than 14 fonts/segoeui.ttf Is there any possibilty to solve this ? – Yawar Nov 01 '14 at 14:18
  • I am facing the same issue. Any Solution? – Nithinjith May 13 '16 at 08:18
  • @adnan-amjad I can't get this to work, unfortunately. Is an external library (such as Caligraphy) needed for this to work? – ra3o.ra3 Apr 05 '17 at 19:11
0

You can follow this. In your Application class:

Typeface fontHelvetica = Typeface.createFromAsset(this.getResources().getAssets(), "font/helvetica_font.ttf");
injectTypeface("helvetica", fontHelvetica);

private boolean injectTypeface(String fontFamily, Typeface typeface) {
        try {
            Field field = Typeface.class.getDeclaredField("sSystemFontMap");
            field.setAccessible(true);
            Object fieldValue = field.get(null);
            Map<String, Typeface> map = (Map<String, Typeface>) fieldValue;
            map.put(fontFamily, typeface);
            return true;
        } catch (Exception e) {
            Log.e("Font-Injection", "Failed to inject typeface.", e);
        }
        return false;
    }

After that you can use in XML as per mentioned in your Question.

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
-1

You can do that by making CustomTextView that extends TextView like this as shown below

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyTextView(Context context) {
    super(context);
    init();
}

private void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                           "your_font.ttf");
    setTypeface(tf);
}

}

But remember, there is serious memory concers in older android version. Refer this issue

https://code.google.com/p/android/issues/detail?id=9904

Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39