others-How to make a reference to other resource names in android xml resource file

1. Purpose

In this post, I would demonstrate how to make a reference to other resource names in android xml resource files.

2. The problem and solution

2.1 The problem

Let’s say there is a string resource named app_name in string.xml:

<string name="app_name">SMS Scheduler</string>

Then in another string, it use the app_name again:

<string name="welcome">Welcome to SMS Scheduler!</string>

You can see that the app_name is repeated in strings. How to avoid repeating in android string resource files?

2.2 The solution

TL;DR, here is the solution:

<!DOCTYPE resources [
	<!ENTITY appname "SMS Scheduler">
	<!ENTITY appvendor "bswen.com">
<string name="app_name">SMS Scheduler</string>
<string name="welcome">Welcome to &appname;!</string>

2.3 Why does it work?

What is DOCTYPE resources?

A document type declaration, or DOCTYPE, is an instruction that associates a particular XML or SGML document (for example, a webpage) with a document type definition (DTD) (for example, the formal definition of a particular version of HTML 2.0 - 4.0).

<!DOCTYPE> is not mandatory in XML.

We use <!DOCTYPE resources because our android xml is started with <resources>

And you can see that we are using <!ENTITY>, what is entity?

Entities reference data that act as an abbreviation or can be found at an external location. Entities help to reduce the entry of repetitive information and also allow for easier editing (by reducing the number of occurrences of data to edit). There are two types of entity declarations: GENERAL entity declarations, and PARAMETER entity declarations.

For example, we can use <!Entity> to avoid repetitive elements in xml:

<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
  <!ELEMENT author (#PCDATA)>
  <!ENTITY js "Jo Smith">

The format of entity is as follows:

<!ENTITY name "entity_value">
  • Entity value: any character that is not an ‘&’, ‘%’ or ‘ “ ‘, a parameter entity reference (‘%Name;’), an entity reference (‘&Name;’) or a Unicode character reference.

3. Summary

In this post, I demonstrated how to reference to other resource names in android xml, we should use XML entities to avoid repeat yourself. That’s it, thanks for your reading.