Strings are a sequence of characters that are used in several scenarios. Android enables developers to use three types of
string resources, namely
- Plain Strings.
- String formats.
- Styled texts.
Plain Strings:
Plain string resources are declared within res/values/strings.xml These are normal text strings with no
codes or styles.
When a new project is created in Eclipse, the ADT automatically generates a sample strings.xml file containing the appname
and hello.
The ADT also adds this to the string name in layout, main.xml and in AndroidManifest.xml.
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SampleAPP!</string>
<string name="app_name">SampleAPP</string>
</resources>
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Strings referenced from the res/layout/main.xml.
Strings can also be referenced from the java code.
The first string is the text of a textview defined in res/layout/main.xml file.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.schogini.SampleAPP" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleAPP" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Referencing Strings:
@string/Resource_Name in xml files.
R.string.Resource_Name in java files.
R.java:
package com.schogini.SampleAPP;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon =0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
strings.xml is never accessed or referenced directly in Java. Android converts them to R.Java and we access those stringsvia the R.java file.
The main advantage is that there is no need to parse xml file, android does this.String arrays:
String arrays can also be created.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
Custom String Resources:
Our own custom resource strings file can also be created.
To create go to res/values/ directory in Package Explorer in Eclipse IDE,Right click and select,
New–>File and Enter a name for the file with Extension. (See Image Attached).
Resources can be added manually by choosing the myStrings.xml tab or by clicking “Add” button and adding the name and the
value of the resource.

Custom String Resources in Android
Formatted strings:
The Dalvik Virtual Machine offers string formats, which provide placeholders representing data to be replaced at runtimeby variables.
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the othertype of enclosing quotes. For example, here are some stings that do work and those don’t work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don't work</string>
Formatting strings
Android allows strings to be formatted as in String.format(String, Object…) in java.lang.string Package. This can beaccomplished easily. The following are a few examples of exceptions.
<string name=”welcome_messages”>Hello, %1$s! You have %2$d new messages.</string>In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the
string with arguments from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages),username, mailCount);
Styled strings:
Android support the following HTML elements:
- <b> for bold text.
- <i> for italic text.
- <u> for underline text.
To create a styled text resource that is also used as a format string, HTML tags can be used. Normally, this won’t workbecause the String.format(String, Object…) method will strip all the style information from the string. The work-around
to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.
For example:
Store your styled text resource as an HTML-escaped string:
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
In this formatted string, a <b> element is added. Notice that the opening bracket is HTML-escaped, using the
< notation.
Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styledtext:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
Because the fromHtml(String) method will format all HTML entities, be sure to escape any possible HTML characters in the
strings you use with the formatted text, using htmlEncode(String). For instance, if you’ll be passing a string argument
to String.format() that may contain characters such as “<” or “&”, then they must be escaped before formatting, so
that when the formatted string is passed through fromHtml(String), the characters come out the way they were originally
written.
For example:
String escapedUsername = TextUtil.htmlEncode(username);
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
CharSequence styledText = Html.fromHtml(text);