Android – Using html on strings.xml

The architecture of an android application consists on a series of directories and files. Besides the java source code, resource files plays an important role inside an android application. In android, resource files are represented as xml files.

One of the most important resource file is strings.xml usually located on res/values/strings.xml. This xml file is meant to store all the strings of an android application in order to allow the developers to manage them in the simpliest way possible. In android development in fact, is a bad behavior hardcoding a string used for showing something through the ui.

In addition to this, storing strings inside strings.xml allow developers to make them translatable in order to make available the application into any language.

How it works?

Here is a simple example of a strings.xml file:

The element’s name is used as the resource ID and can be referenced in two different ways:

In Java: R.string.string_name
In XML:@string/string_name

For example this layout XML applies a string to a View:

While this java code retrieves a string:

Storing html inside a strings.xml

Using string containing html code is pretty simple in fact it’s only needed to use a CDATA (Unparsed Character Data) section with html code inside:

This is an html code.

]]>

For more about string resources on android take a look to documentation here.