1

In my application used ClementePDaa-Hairline.ttf fonts. ClementePDaa-Hairline.ttf fonts work perfectly in android 4.3 and below. but android 4.4 this fonts not display in application. Help me why ClementePDaa-Hairline.ttf fonts not working in android 4.4 and 4.4.2

In android 4.3 and below

In android 4.3 and below version it display ClementePDaa-Hairline.ttf fonts in application.

In android 4.4.0 and above

In android 4.4 and above version ClementePDaa-Hairline.ttf fonts not display in application.

public class TypefaceClass {


    public static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),
                            assetPath);
                    cache.put(assetPath, t);
                } catch (Exception e) {                     
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }

}





// IN ACTIVITY

TextView textview = (TextView)findViewById(R.id.t1);
textview.setTypeface(TypefaceClass.get(getApplicationContext(),
                "fonts/ClementePDaa-Hairline.ttf"));
Slk
  • 205
  • 5
  • 12

5 Answers5

4

Please check this out. It may help you. Create a typeface class as like this.

public class TypefaceClass {


        public static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

        public static Typeface get(Context c, String assetPath) {
            synchronized (cache) {
                if (!cache.containsKey(assetPath)) {
                    try {
                        Typeface t = Typeface.createFromAsset(c.getAssets(),
                                assetPath);
                        cache.put(assetPath, t);
                    } catch (Exception e) {                     
                        return null;
                    }
                }
                return cache.get(assetPath);
            }
        }

}

Create a font folder in your assets and put your font in that folder. Then setTypeface by using the above typeface class.

textView.setTypeface(TypefaceClass.get(context,
                "font/your_font.ttf"));

EDIT:

Please try to use .otf file instead of .ttf file. It will surely solve your problem. There is some issue with .ttf file from 4.4 above. So try to get the .otf file of font.

RIJO RV
  • 806
  • 6
  • 18
  • Thanks. Right now finding ClementePDaa-Hairline.otf font. i will check after find and let you know. – Slk Dec 30 '13 at 08:27
2

use this to set font on device sdk version

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
                if (currentapiVersion >=
                        android.os.Build.VERSION_CODES.KITKAT) {
                    font = Typeface.DEFAULT;
                }else{
                    font = Typeface.createFromAsset(context.getAssets(), "CALIBRI.TTF");
                }
Android Geek
  • 8,956
  • 2
  • 21
  • 35
Abhijeet
  • 392
  • 2
  • 13
1

Apparently this is a bug in KitKat and has been fixed in an internal tree.

M D
  • 47,665
  • 9
  • 93
  • 114
0

I had the same problem with custom font, it works fine on all versions of Android except the 4.4.2. I converted my ".ttf" to ".otf" and now it's OK.

Arthurius
  • 67
  • 1
  • 8
0

If you are setting the text to the TextView by the method

textView.setText(Html.fromHtml(text));

The html styling of the text (even if is empty might override the previous font you set on the TextView. To fix it you could override the method setText

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if (fontType != null) {
        initTypeface();
    }
 }

My complete solution so you can use custom TextView on code and on xml is (assuming that you have the font files under assets/font/ folder

public class CustomTextView extends TextView {

String TAG = CustomTextView.class.getSimpleName();

public enum FontType {
    MONTSERRAT_BOLD, MONSERRAT_REGULAR, PROXIMANOVA_REGULAR, PROXIMANOVA_REGULAR_ITALIC
}

/* Default font */
private FontType fontType = FontType.MONSERRAT_REGULAR;

/* Constructor from xml */
public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initTypeface(attrs);

}
/* Constructor from xml */
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initTypeface(attrs);
}
/* Constructor from code */
public CustomTextView(Context context) {
    super(context);
    initTypeface();
}
/* Constructor from code */
public CustomTextView(Context context, FontType fontType) {
    super(context);
    this.fontType = fontType;

    initTypeface();
}

private void initTypeface() {

    setLineSpacing(10, 1);
    String fontName = null;
    switch (fontType) {

        case MONTSERRAT_BOLD:

            fontName = "MontserratBold.otf";
            break;
        case MONSERRAT_REGULAR:
            fontName = "MontserratRegular.otf";
            break;
        case PROXIMANOVA_REGULAR:
            fontName = "ProximaNovaRegular.otf";
            break;
        case PROXIMANOVA_REGULAR_ITALIC:
            fontName = "ProximaNovaRegularItalic.otf";
            break;

        default:
            fontName = "MontserratRegular.otf";
    }
    if (!TextUtils.isEmpty(fontName)) {

        Typeface t = Typeface.createFromAsset(getContext().getAssets(), "font/" + fontName);
        this.setTypeface(t);
    }


}

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if (fontType != null) {
        initTypeface();
    }
}

private void initTypeface(AttributeSet attrs) {


    TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
    if (a != null) {
        int enumPosition = a.getInteger(R.styleable.CustomTextView_fontType, 0);

        this.fontType = FontType.values()[enumPosition];      

        initTypeface();
    }


 }

}

To be able to instantiate the class on xml you need to create an enum under values attrs.xml

<declare-styleable name="CustomTextView">
    <attr name="fontType" format="enum">
        <enum name="monserrat_bold" value="0" />
        <enum name="monserrat_regular" value="1" />
        <enum name="proximanova_regular" value="2" />
        <enum name="proximanova_italic" value="3" />

    </attr>
</declare-styleable>

Then you can instantiate your view on xml

 <com.yourpackagename.views.textViews.CustomTextView      xmlns:custom="http://schemas.android.com/apk/res-auto"
            android:id="@+id/test_name_text_view"
            custom:fontType="monserrat_bold"
            style="@style/AppFont.Assessment.TestTitle"
            android:layout_marginStart="@dimen/top_bar_margin_start"
            android:layout_marginTop="@dimen/top_bar_margin_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

or from the code

CustomTextView textView = new CustomTextView(context, CustomTextView.FontType.MONSERRAT_REGULAR);
pleonasmik
  • 779
  • 10
  • 16