Custom font android : Android Tutorial

Custom font in Android Android Tutorial

Android comes with a nice default font but in some cases you might want to use a different font. In this post I will show you how you can use a custom font android in your apps.

Fastest Way to use custom font android

  • Put your font file in asset folder

Custom font android Android Teachers

  • Create Typeface Object

 

  • Set Typeface to TextView/EditText/Button etc…

Follow full tutorial for batter practice.

Include font as resource

The custom font needs to be included in the app as a resource. This resource should be in the assets folder of your project, to keep the project clean it would be useful to create a subfolder for fonts. Create the following folder in your project “assets/fonts”.
Now that the folder is created the font can be added to the project. Copy the custom font to the “assets/font” folder.

Note that the assets folder is not copied from a library project, if you have any libraries that get’s resources from the assets folder you must include these assets in the final published project. http://developer.android.com/tools/projects/index.html

Set typeface on Textview

Using the custom font with an adapter

You might wonder how to use your custom font with a list view. The answer lies in the usage of a custom adapter.
I Will use the SimpleCursorAdapter for this example, you can use this technique with most other adapters.

First we need to define the new custom class:

Now we add a bindview function to our class. This function is made to give you control over the item within a list. You can also use this function to make more complex field bindings when working with data, for example you could change the color of a text view or hide an image. You can even use this function to inflate different layouts depending on the data. But that would be a whole article on it’s own, for now we will focus on altering the view as inflated by the adapter itself. We will change the font for a textview in the inflated listview item to our custom (Roboto-Bold) font.

The previous example will work but it will also be a bit inefficient because the bindView is triggered every time a listview item is being set up and we are loading the custom font in the same function. If we move the loading of the font to the constructor of the custom adapter it will be more efficient.

Creating a helper class

If you are using one font a lot in your project(s) it might be useful to create a helper class to quickly apply your font to a view. The added benefit of this class is that it will give better performance because we can cache the typeface and only load it one.

We start by creating a helper class for our custom font, and create an apply font function

Next we will add a getter for our typeface (singleton), if the typeface has not been loaded yet it will do so otherwise it will return the cached instance.
The added benefit from this function is that the typeface will only be loaded on demand, if not applied it will not load the typeface.

The last step is to apply the custom font android to the textview. it’s Best practice for using custom font android

Here is a small example on how to use the helper class.

More information

For more information see: http://developer.android.com/reference/android/graphics/Typeface.html

That was it for this post, I hope it is useful to you. comment and subscibe for more
Till next time.

168total visits,2visits today

Related posts

Leave a Comment