VOOZH about

URL: https://www.javacodegeeks.com/2013/01/android-textview-with-custom-fonts.html

โ‡ฑ Android TextView with custom fonts


While developing an android app I found that everytime I was trying to use a custom font on a TextView I copied the same lines:
 
 
 
 
 
 
 
 
 

TextView textview = (TextView) findViewById(R.id.text);
textview.setTypeface(...)

Obviously, thatโ€™s unnecessary and repetitive. Why not just create a custom view? And more, why not adding the font through xml code?

public class CustomTextView extends TextView {

 private Context mContext;
	private String mFont;
	
	public CustomTextView(Context context) {
		super(context, null);
		mContext = context;
		init();
	}

	public CustomTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		TypedArray a = context.getTheme().obtainStyledAttributes(
		 attrs,
		 R.styleable.CustomButtom,
		 0, 0);
		try {
			mFont = a.getString(R.styleable.CustomButtom_font);
		} finally {
			a.recycle();
		}
		init();
	}
	
	private void init() {
		if (mFont != null) {
			setTypeface(FontsUtils.get(mFont));
		}
	}

}

We just have to extend from TextView and read from the attribute set the font string declared on its styleable resource. Just create an attrs.xml (or use an existing one) and add the following:

<declare-styleable name="CustomTextview">
 <attr name="font" format="string" />
</declare-styleable>

Now, you could declare on your xml layout like this:

<LinearLayout
 android:id="@+id/comments"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
 
 <com.parkuik.android.ui.CustomTextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/no_comments" />
</LinearLayout>

Reference: Android TextView with custom fonts from our JCG partner Javier Manzano at the Javier Manzanoโ€™s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

๐Ÿ‘ Photo of Javier Manzano
Javier Manzano
January 24th, 2013Last Updated: January 24th, 2013
3 87 1 minute read

Javier Manzano

Javier is an Android and node.js developer. Working day to day on new pet projects. Founder of Parkuik and Codeely.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Bill Mote
13 years ago

You didnโ€™t include your FontsUtils() class in which, I assume, youโ€™re actually doing the Typeface.createFromAsset() call to get the font from fonts/font_name.*tf

0
Reply
opasam
12 years ago

Could you post your FontsUtils class please ?

0
Reply
android user
11 years ago

from where did we get โ€œCustomButtom_fontโ€ ?

thank you

0
Reply
Back to top button
Close
wpDiscuz