The easiest way to accomplish this is to package the desired font(s) with your application. To do this, simply create an assets/
folder in the project root, and put your TrueType (TTF) fonts in the assets. You might, for example, create assets/fonts/
and put your TTF files in there.
Then, you need to tell your widgets to use that font. Unfortunately, you can no longer use layout XML for this, since the XML does not know about any fonts you may have tucked away as an application asset. Instead, you need to make the change in Java code:
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.custom);
Typeface face = Typeface.createFromAsset(getAssets (),
'fonts/HandmadeTypewriter.ttf');
tv.setTypeface(face);
}
}
Here we grab the TextView
for our “custom” sample, then create a Typeface
object via the static createFromAsset()
builder method. This takes the application’s AssetManager
(from getAssets()
) and a path within your assets/
directory to the font you want.
Then, it is just a matter of telling the TextView
to setTypeface()
, providing the Typeface
you just created. In this case, we are using the Handmade Typewriter[15] font (see Figure 12-1).

Figure 12-1.
Note that Android does not seem to like all TrueType fonts. When Android dislikes a custom font, rather than raise an Exception
, it seems to substitute Droid Sans (“sans”) quietly. So, if you try to use a different font and it does not seem to be working, it may be that the font in question is incompatible with Android, for whatever reason.
Also, you are probably best served by changing the case of your font filenames to be all lowercase, to match the naming convention used in the rest of your resources.
Also note that TrueType fonts can be rather pudgy, particularly if they support an extensive subset of the available Unicode characters. The Handmade Typewriter font used here runs over 70KB; the DejaVu free fonts can run upwards of 500KB apiece. Even compressed, these add bulk to your application, so be careful not to go overboard with custom fonts, or your application could take up too much room on your users’ phones.
CHAPTER 13
Embedding the WebKit Browser
Other GUI toolkits let you use HTML for presenting information, from limited HTML renderers (e.g., Java/Swing, wxWidgets) to embedding Internet Explorer into .NET applications. Android is much the same, in that you can embed the built-in Web browser as a widget in your own activities, for displaying HTML or full-fledged browsing. The Android browser is based on WebKit, the same engine that powers Apple’s Safari Web browser.
The Android browser is sufficiently complex that it gets its own Java package (android.webkit
), though using the WebView widget itself can be simple or powerful, based upon your requirements.
A Browser, Writ Small
For simple stuff, WebView is not significantly different than any other widget in Android — pop it into a layout, tell it what URL to navigate to via Java code, and you’re done.
For example, WebKit/Browser1
is a simple layout with a WebView. You can find WebKit/Browser1
along with all the code samples for this chapter in the Source Code area at http://apress.com.
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:orientation='vertical'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
>
<WebView android:id='@+id/webkit'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
/>
</LinearLayout>
As with any other widget, you need to tell it how it should fill up the space in the layout (in this case, it fills all remaining space).
The Java code is equally simple:
package com.commonsware.android.webkit;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class BrowserDemo1 extends Activity {
WebView browser;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
browser = (WebView)findViewById(R.id.webkit);
browser.loadUrl('http://commonsware.com');
}
}
The only unusual bit with this edition of onCreate()
is that we invoke loadUrl()
on the WebView
widget, to tell it to load a Web page (in this case, the home page of some random firm).