Android Custom Fonts TextView

Android Custom Fonts TextView

In android, you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder. In Eclipse the assets folder is by-default created but in Android Studio you have to create assets folder.

Note : assets folder creating path in Android Studio app/source/main/assets/fonts.

After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. You can achieve creating custom font in just two simple steps:

1.      Call static method of Typeface class createFromAsset() to get your custom font from assets.

2.      Set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that.

Apart from these Methods, there are other methods defined in the Typeface class , that you can use to handle Fonts more effectively that are shown below :

MethodDescription
create(String familyName, int style)Create a Typeface object given a family name, and option style information.
create(Typeface family, int style)Create a Typeface object that best matches the specified existing Typeface and the specified Style.
createFromFile(String path)Create a new Typeface from the specified font file.
defaultFromStyle(int style)Returns one of the default Typeface objects, based on the specified style.
getStyle()Returns the Typeface's intrinsic style attributes.

Example

In this tutorial, we are going to learn how to use Custom Font in Android. This article will show the use of two Custom TextView with different font and how to display rupee symbol using Rupee font.

Video Demo

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res=>values=strings.xml and add the following strings to it.

3. Now create a MyApplication.java class and extend it with Application. Inside this class create all your fonts typefaces. The use of this class that this will create Typefaces globally use in your activity.

4. Now update your AndroidManifest.xml file by adding  android:name=”.MyApplication” inside application tag.

5. Now i am going to create two different TextView class for using it in layout. One is for OpenSansLight Font and another one is for OpenSansBold Font.

CustomFontTextView.java

CustomFontTextView.java

In above both classes there is init() method that is used for setting typeface over the TextView.

6. Now create a layout xml file naming activity_main.xml and add the following code. In this layout i had taken both custom TextView that i had created in above step and two more for setting typeface programatically.

7. Finally come to your MainActivity.java and add the following code. In this code i am setting typeface over both TextView. For Rupee font to show rupee symbol we need to use “`” this symbol.

8. Finally, all done you can use above steps for using custom font in your app.

Thanks. :)

Download Source Code – >

 

 

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)