Understanding String Resources

Android offers three types of string resources: Plain Strings, string formats and styled texts

Plain string resources can be declared in res/values/strings.xml

If you create an Android application [for example call it HelloAndroid] and just before adding anything, browse to res/values/strings.xml it will be like this:

Hello World, HelloAndroid!
HelloAndroid

This is a pretty example of Plain text resources. The resource with name=”app_name” is the name of the application that appears when you deploy the application to the phone, it’s referenced in the AndroidManifest.xml file in the application tab. You can change it as you want.
Now let’s add two entries in the file to see how can we use plain text resources within the application
This is referenced from the res/layout/main.xml
This is referenced from the code
The first string will be the text of a text view defined in res/layout/main.xml file




See that to reference the first string we use the @string/[Resource Name] convention.
The second resource will be referenced from the code file of the activity like this
TextView txtHeader2=(TextView)findViewById(R.id.txtHeader2);
txtHeader2.setText(getString(R.string.plainResource2));
if you open R.java file of your project you will find that Android has generated a class called string with members referencing to your string resources:
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int plainResource1=0x7f040002;
public static final int plainResource2=0x7f040003;
}

Also notice the way you access the string resources in Android, you don’t open the strings.xml file and parse it t extract the values you want to reference- instead you access them through the R.string class defined in R.Java and Android does the rest for you.

Also another interesting feature is that you can define your own resources file, go to res/values/ directory, right click>New>File and call the file for example CustomStrings
You will see something like this:

You can define resources manually by choosing the CustomStrings.xml tab or by using clicking Add button and adding the name and the value of the resource.
I will add a resource with the name CustomString and value this is a custom string and reference them from the layout like this


Or from the code like this:
TextView txtHeader3=(TextView)findViewById(R.id.txtHeader3);
txtHeader3.setText(getString(R.string.CustomString));

String format Resources:

the dalvik vm offers string formats whih provide placeholders representing data to be replaced at runtime by variables
an example of a string format resource is:
This is resource for %1$s
the %1$s is the place holder that would be replaced by a variable string.
and from your code you can use it like this:
//String format resource
TextView txtHeader4=(TextView)findViewById(R.id.txtHeader4);
String strFormat=getString(R.string.StringFormat);
String header=String.format(strFormat, "Giving an example of string format resource");
txtHeader4.setText(header);

and the text view will have a text equal to This is resource for Giving an example of string format resource.

Styled text Resources:
You can use string resources styled with these three HTML tags:
<b>,<u> and <i>

You can define the a string as follows:
This is an <u>example</u> of <i>Styled</i> <b>Resources</b> 

And use it from the code as this:
TextView txtHeader5=(TextView)findViewById(R.id.txtHeader5);
txtHeader5.setText(R.string.StyledResource);

notice that we use setText() method by calling the string resource directly.
if we use the getString() method it will display the string without styling
we can use the HTML styled text also by using spanned.
Spanned textSpan = android.text.Html.fromHtml(htmlTaggedString);
//Set it in a text view
textView.setText(textSpan);

Summary
Android provides three types of string resources: Plain strings, String formats which provide place holders to be replaced by variables in runtime and Styled text resources which provide styling with three standard HTML tags
Download a demo application here

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.