Friday, April 8, 2011

Custom Fonts on Android

Sometimes the stock fonts on Android may not cut it when trying to create the look and feel of your app so embedding a custom typeface file will be the way to go.
First, you may need some cool fonts so I recommend you to check http://www.dafont.com and get some free fonts for you project (read the license agreement before using it in your phone).

Once you have picked your fonts you need to copy it in the assets folder in your application; I normally create a subfolder called fonts but it is not required.












Then OnCreate add the following lines of code and you are ready to go:

TextView text = (TextView)findViewById(R.id.labelTitle);
Typeface customFont = Typeface.createFromAsset(getAssets(),"font/custom.ttf");
       
text.setTypeface(customFont);

In the sample above I am setting the "labelTitle" TextView to use my custom.ttf files

Yes,  that’s all.

No comments:

Post a Comment