30.04.2013 Views

Android - CommonsWare

Android - CommonsWare

Android - CommonsWare

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Working with Resources<br />

/><br />

<br />

<br />

<br />

<br />

As you can see, it is just a button, a field, and a label. The intent is for<br />

somebody to enter their name in the field, then click the button to cause<br />

the label to be updated with a formatted message containing their name.<br />

The Button in the layout file references a string resource (@string/btn_name),<br />

so we need a string resource file (res/values/strings.xml):<br />

<br />

<br />

StringsDemo<br />

Name:<br />

My name is &lt;b&gt;%1$s&lt;/b&gt;<br />

<br />

The app_name resource is automatically created by the activityCreator<br />

script. The btn_name string is the caption of the Button, while our styled<br />

string format is in funky_format.<br />

Finally, to hook all this together, we need a pinch of Java:<br />

package com.commonsware.android.strings;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.text.TextUtils;<br />

import android.text.Html;<br />

import android.view.View;<br />

import android.widget.Button;<br />

import android.widget.EditText;<br />

import android.widget.TextView;<br />

public class StringsDemo extends Activity {<br />

EditText name;<br />

TextView result;<br />

263<br />

Subscribe to updates at http://commonsware.com Special Creative Commons BY-SA 3.0 License Edition

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!