Pages

Monday, July 28, 2014

Branding in Android: Custom Typefaces

Normally, since this is my first post on DevFlow, I would go into introducing myself, my history, accomplishments, projects, etc. But I would rather just get started (we can become best friends later).


Let’s talk about design.




One overly important attribute of design is branding. The choices you make in displaying your application/website/project to your user has an impact.  Colors, Names, Icons, Typefaces, (and many other attributes) define your application for the user.


Your users recall your application based upon your branding.


Designing is one thing, but implementing these attributes (typefaces, icons, colors, etc.) programmatically can be a hassle. So, let’s start with adding a custom typeface.

Typefaces in Android


Like everything in Android, there are a multiple ways to accomplish the same task. Some tend to be better than others, but that is dependent upon the situation. For our purposes, let us suppose that we are creating a new application, with a custom typeface that we want to be in most (if not all) of our TextViews.

Grab your typeface


First things first, grab your font. Google Fonts is a great place to find free fonts to use in your application (I chose Raleway). Download the zip of your selected font and extract the archive. Copy your desired .ttf into the fonts folder of your project (create this folder in the assets folder of your project). Finally, rename your font to be completely lowercase and rid of any special characters XML cannot understand. For instance, my typeface was named, "Raleway-Light.ttf". I changed that to "raleway_light.ttf"


Extending a View


Time for some code. Create a new package in your project called "Views" (this is where we will create our new TextView with our custom typeface. In the "Views" folder, create a new class that extends TextView. I titled mine, "RaleWayTextView" because that is the font I am using.

Create a constructor with initialization:







Create a constructor without initialization:







Finally, add your initialization method:











Declare it Styleable

Next, we need to create an attrs.xml file in our values folder, located in the res folder.



This file will consist of one line, letting Android know that we can style our new TextView. 
<declare-styleable name="RalewayTextView"/>

Adding Your New View

Now, all we have left to do is add our new TextView. We do this the same way that you add any other view to a layout. Enter your layout file, then simply add your view. Mine looks like this: 
















There are a few extra lines in there you don't necessarily need (such as padding), but it does give some breathing room to your view.


Wrapping Up

You now have everything setup to use your custom TextView in your application. Once you've added the view to your layout file, you should get a similar result to what you see below (with your own typeface of course)




Remember, all of this source code is available to you on github. There you will find an entire application dedicated to the tutorials in this blog. Feel free to clone and import that project on your machine. DevFlow is also on the Play Store.

Next time, my good friend Shirwa will cover another very important section of Branding in Android, Colors!

About Me

My photo
http://about.me/anthonykiniyalocts/#