Example Code

Example Java code

// Assign a styled string resource to a TextView // on the current screen.

CharSequence str = getString(R.string.styled_welcome_message); TextView tv = (TextView)findViewByID(R.id.text); tv.setText(str);

Example XML code

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:textAlign="center"

android:text="@string/simple_welcome_message"/>

Using Styled Text as a Format String

Sometimes you may want to create a styled text resource that is also used as a format string. This cannot be done directly because there is no way of passing the styled text as the format string argument of String.format() without stripping out the style information. The workaround is to store the style tags as escaped HTML tags, and then convert the escaped HTML string into a styled text after formatting has taken place.

To use styled text as a format string, do the following.

1. Store your styled text resource as an escaped string, so that the HTML tags in your text resource are not interpreted as if they were XML tags:

<resources>

<string name="search_results_resultsTextFormat">%1$d results for &lt;b>&amp;quot;%2$s&amp;quot;&lt;/b></string> </resources>

In this example the format string has two arguments: %1$d is a decimal number, %2$s is a string.

2. Make sure any String arguments are properly escaped if they might contain '<' or '&' characters. The htmlEncode(String) method will do this:

String escapedTitle = TextUtil.htmlEncode(title);

3. Use String.format() to format the HTML text, then use fromHtml(String) to convert the HTML text into styled text:

String resultsTextFormat =

getContext().getResources().getString(R.string.search_results_resultsTextFormat );

String resultsText = String.format(resultsTextFormat, count, escapedTitle); CharSequence styledResults = Html.fromHtml(resultsText);

You can create common dimensions to use for various screen elements by defining dimension values in XML. A dimension resource is a number followed by a unit of measurement. For example: 10px, 2in, 5sp. Here are the units of measurement supported by Android:

Pixels - corresponds to actual pixels on the screen.

Inches - based on the physical size of the screen.

Millimeters - based on the physical size of the screen.

Points - 1/72 of an inch based on the physical size of the screen.

Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Dimension values are not normally used as raw resources, but rather as attribute values in XML files. You can, however, create plain resources containing this data type.

Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <dimen> tags.

Resource source file location: res/values/dimens.xml (File name is arbitrary; standard practice is to put all dimensions in one file devoted to dimensions.)

Compiled resource datatype: Resource pointer to a dimension.

0 0

Post a comment