0

My Custom Font class

public class CustomFontText extends TextView { /* * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced. */ private static Typeface mTypeface; public CustomFontText(final Context context) { super(context, null); } public CustomFontText(final Context context, final AttributeSet attrs) { super(context, attrs, 0); readAttrs(context, attrs); } public CustomFontText(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); readAttrs(context, attrs); } private void readAttrs(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); // Read the title and set it if any String fontName = a.getString(R.styleable.CustomTextView_fontname); if (fontName != null) { // We have a attribute value if (mTypeface == null) { mTypeface = Typeface.createFromAsset(context.getAssets(), fontName); setTypeface(mTypeface); } } // a.recycle(); } } 

Applying in XMl file

<somepackage.CustomFontText android:id="@+id/details" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ewfewfewqfewfwef" custom:fontname="Roboto-Regular.ttf" /> 

It is not giving any kind of error but I am not able to view any changes in the textview. Changing the fontname makes no difference.

6
  • check it this link:stackoverflow.com/questions/6926263/… Commented Aug 5, 2016 at 8:12
  • did you check if fontName is not null? Do you have the fonts in the assets? Commented Aug 5, 2016 at 8:12
  • @Blackbelt I have put all the fonts in my asset folder.And also I debug my code to check if it's null or not. Commented Aug 5, 2016 at 8:16
  • Just a hunch, but you could try moving the setTypeface(mTypeface); line outside the check for mTypeface == null. Commented Aug 5, 2016 at 9:03
  • 1
    @Soham Glad it's working now! I've posted the solution as an answer. Commented Aug 5, 2016 at 9:14

4 Answers 4

2

Moving the code setTypeface(mTypeface); outside the check for mTypeface == null should solve the issue. So the code should look like this:

if (mTypeface == null) { mTypeface = Typeface.createFromAsset(context.getAssets(), fontName); } setTypeface(mTypeface); 

This is because mTypeface is declared static and such all CustomFontText instances share the same typeface (which makes sense for caching). If setTypeface is called inside the check though, it will only get applied once, when the typeface is first loaded.

Sign up to request clarification or add additional context in comments.

Comments

0

Actually I don't know why yours not working, however, alternatively you can use Calligraphy by chrisjenx. I used it in one of my projects and it works great!

1 Comment

I don't want to use can libraries.Some code tweaking would be helpful
0

Create folder fonts in the assets folder of your project and instead of using just the font name for custom:fontname, use the path to the file.

custom:fontname="fonts/Roboto-Regular.ttf" 

Comments

0

Add ttf or otf file in your assets folder.

Create custom class extends with TextView

public class CustomText extends TextView { public CustomText (Context context) { super(context); createTextView(context, null); } public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); createTextView(context, attrs); } public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); createTextView(context, null); } private void createTextView(Context context, AttributeSet attrs) { String fontName; TypedArray typedArray; if (isInEditMode()) return; if (attrs != null) { typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTypeFace, 0, 0); fontName = typedArray.getString(R.styleable.FontTypeFace_typeface); setFontTypeFace(context, fontName); typedArray.recycle(); } } private void setFontTypeFace(Context context, String fontName) { if (fontName != null) { Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName); setTypeface(typeface); } } } 

declare stylable in your attrs file :

<declare-styleable name="FontTypeFace"> <attr name="typeface" format="string" /> </declare-styleable> 

Create control using custom textview in xml file :

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="horizontal" android:weightSum="1"> <com.Widget.CustomTextView android:id="@+id/txt_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/dimen_text_size_12" app:typeface="@string/thin" /> </LinearLayout> 

Just add the assets file name inside string.xml file

<!--String for assets font type file name --> <string name="bold">bold.otf</string> <string name="light">light.otf</string> <string name="medium">medium.otf</string> <string name="regular">regular.otf</string> <string name="regular_italic">regular_italic.otf</string> <string name="semi_bold">semibold.otf</string> <string name="thin">thin.otf</string> 

1 Comment

What is Custom_TextView_typeface?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.