String Resources
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings
- String
- XML resource that provides a single string.
- String Array
- XML resource that provides an array of strings.
- Quantity Strings (Plurals)
- XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments. For information about styling and formatting strings, see the section about Formatting and Styling.
String
A single string that can be referenced from the application or from other resource files (such as an XML layout).
- FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The<string>
element'sname
will be used as the resource ID.- COMPILED RESOURCE DATATYPE:
- Resource pointer to a
String
. - In Java:
R.string.string_name
In XML:@string/string_name
- SYNTAX:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="string_name">text_string</string> </resources>
- ELEMENTS:
- EXAMPLE:
- XML file saved at
res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="logiclab">LogicLab Solutions</string> </resources>
This layout XML applies a string to a View:<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/logiclab" />
This application code retrieves a string:String string =
getString
(R.string.logiclab);
- RESOURCE REFERENCE:
String Array
An array of strings that can be referenced from the application.
FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The<string-array>
element'sname
will be used as the resource ID.- COMPILED RESOURCE DATATYPE:
- Resource pointer to an array of
String
s. - RESOURCE REFERENCE:
- In Java:
R.array.string_array_name
- SYNTAX:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="string_array_name"> <item>text_string</item> </string-array> </resources>
- ELEMENTS:
- EXAMPLE:
- XML file saved at
res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="vehicle_array"> <item>Bike</item> <item>Car</item> <item>Jeep</item> <item>Bus</item> </string-array> </resources>
This application code retrieves a string array:Resources res =
getResources()
; String[] planets = res.getStringArray
(R.array.vehicle_array);
Quantity Strings (Plurals)
Android supports Plurals. Plurals are XML based resources which allow to handle different quantities. This way you can select the right text based on the quantity. In your XML file you specify values for the quantities “zero”, “one”, “two”, “many”, “few”, “many”, “other” and in your code you use the method getQuantityString() to get the correct value. You can also format strings. If now Strings are formated then you pass in the plural resources and the number. If Objects should be used for formating you pass them as additional parameters.
res/values/filename.xml
The filename is arbitrary. The
<plurals>
element's name
will be used as the resource ID.R.plurals.plural_name
<?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="plural_name"> <item quantity=["zero" | "one" | "two" | "few" | "many" | "other"] >text_string</item> </plurals> </resources>
EXAMPLE:
XML file saved at
res/values/strings.xml
:we are going to use “Plurals” tag rather than “String” tag
<
resources
>
<
plurals
name
=
"numberOfBooks"
>
<
item
quantity
=
"one"
>%d book!</
item
>
<
item
quantity
=
"other"
>%d books!</
item
>
</
plurals
>
</
resources
>
This application code
int
booksCount= 3
0
;
String result = getResources().getQuantityString(R.plurals.numberOfBooks, booksCount,booksCount);
textView.setText(result);
Formatting and Styling
Escaping apostrophes and quotes
<string name="good_example">This is a \"good string\".</string>
<string name="good_example1">This\'ll work</string>
Formatting strings
Example<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
The format string has two arguments:
%1$s
is a string and %2$d
is a decimal number.Resources res = getResources()
;
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
Styling with HTML markup
<resources> <string name="welcome">Welcome to <b>Android</b>!</string> </resources>
Supported HTML elements include:
<b>
for bold text.<i>
for italic text.<u>
for underline text.
No comments:
Post a Comment