Using Custom Fonts in Android
By default Android comes with three standard fonts: Droid Sans (default font), Droid Serif, and Droid Sans Mono. They all can be applied to any view that supports styling, such as TextView, Button, by specifying the “android:typeface” attribute in the XML declaration with any of these values: sans, serif, monospace.
1 2 3 4 5 6 7 8 9 |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sans" android:typeface="sans" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Serif" android:typeface="serif" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Monospace" android:typeface="monospace" /> |
Using custom fonts in Android is pretty straightforward. First find a free font and put it in the assets/fonts directory. (It’s not mandatory to have a /fonts directory, but if I have a lot of stuff in the /assets directory I organize them in separate directories). Then get a reference to your TextView and create a Typeface object specifying the font path. Lastly, apply the typeface to the TextView.
In this particular example I used the font: christmaseve.ttf
1 2 3 |
TextView textView = (TextView) findViewById(R.id.textView); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/christmaseve.ttf"); textView.setTypeface(tf); |
RuntimeException: Native typeface cannot be made
If you get this exception while trying to integrate the custom font into your application, make sure the path to the font file is correct, and the font name is spelled correctly. I noticed I was getting this exception when my font path was misspelled, for example writing “.tff” instead of “.ttf”, or forgetting to add the “fonts/” prefix to the path.
Custom font used in this example provided by: http://bythebutterfly.com