@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
name = (EditText)findViewById(R.id.name);
result = (TextView)findViewById(R.id.result);
Button btn = (Button)findViewById(R.id.format);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
applyFormat();
}
});
}
private void applyFormat() {
String format = getString(R.string.funky_format);
String simpleResult = String.format(format,
TextUtils.htmlEncode(name.getText ().toString()));
result.setText(Html.fromHtml (simpleResult));
}
}
The string resource manipulation can be found in applyFormat()
, which is called when the button is clicked. First, we get our format via getString()
— something we could have done at onCreate()
time for efficiency. Next, we format the value in the field using this format, getting a String
back, since the string resource is in entity-encoded HTML. Note the use of TextUtils.htmlEncode()
to entity-encode the entered name, in case somebody decides to use an ampersand or something. Finally, we convert the simple HTML into a styled text object via Html.fromHtml ()
and update our label.
When the activity is first launched, we have an empty label (see Figure 19-1).

Figure 19-1.
However, if we fill in a name and click the button, we get the result seen in Figure 19-2.

Figure 19-2.
Get the Picture?
Android supports images in the PNG, JPEG, and GIF formats. GIF is officially discouraged, however; PNG is the overall preferred format. Images can be used anywhere that requires a Drawable, such as the image and background of an ImageView
.
Using images is simply a matter of putting your image files in res/drawable/
and then referencing them as a resource. Within layout files, images are referenced as @drawable/...
where the ellipsis is the base name of the file (e.g., for res/drawable/foo.png
, the resource name is @drawable/foo
). In Java, where you need an image resource ID, use R.drawable.
plus the base name (e.g., R.drawable.foo
).
If you need a Uri to an image resource, you can use one of two different string formats for the path:
• android.resource://com.example.app/...
, where com.example.app
is the name of the Java package used by your application in AndroidManifest.xml
and ...
is the numeric resource ID for the resource in question (e.g., the value of R.drawable.foo
)
• android.resource://com.example.app/raw/...
, where com.example.app
is the name of the Java package used by your application in AndroidManifest.xml
and ...
is the textual name of the raw resource (e.g., foo
for res/drawable/foo.png
)
Note that Android ships with some image resources built in. Those are addressed in Java with an android.R.drawable
prefix to distinguish them from application-specific resources (e.g., android.R.drawable.picture_frame
).
Let’s update the previous example to use an icon for the button instead of the string resource. This can be found as Resources/Images
. First, we slightly adjust the layout file, using an ImageButton
and referencing a drawable named @drawable/icon
:
<?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'
>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:orientation='horizontal'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
>
<ImageButton android:id='@+id/format'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:src='@drawable/icon'
/>
<EditText android:id='@+id/name'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
/>
</LinearLayout>
<TextView android:id='@+id/result'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
/>
</LinearLayout>
Next, we need to put an image file in res/drawable
with a base name of icon. In this case, we use a 32?32 PNG file from the Nuvola[16] icon set. Finally, we twiddle the Java source, replacing our Button
with an ImageButton
:
package com.commonsware.android.resources;