Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to inject a <string> element into another <string> element in XML?

I would like to know whether there is a way to insert/inject a <string> element defined in an XML file into another <string> element, doing that just with XML.

For example I could have:

<string name="author">Francesco</string>`

and I am looking for something like:

<string name="about_application">Author: @string/author</string>`

so that getString(R.string.about_application) would result in "Author: Francesco".

I know that I could combine the two elements in Java code using String.format(string, formatArgs)like for example:

<string name="author">Francesco</string>
<string name="about_application">Author: %1$s</string>`

and then in code use

String.format(getString(R.string.about_application), getString(R.string.author))

but I would like to do it in XML directly.

Can anyone suggest me a way to do it?

like image 316
Francesco Feltrinelli Avatar asked Sep 15 '10 22:09

Francesco Feltrinelli


People also ask

What is the use of string XML in android?

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.

In which folder can you find the string resource file strings XML?

The XML resource files containing localized strings are placed in subfolders of the project's res folder.

What is the purpose of the Activity_main XML file in the project you created?

Choose as many answers as you see fit. What is the purpose of the activity_main. xml file in the project you created? It provides the theme settings for your app.


2 Answers

If I understand what you are looking to do, then internal (parsed) general entities might help you achieve what you are looking for.

An example of how you can define the value "Francesco" as an entity called "auth" and then use it in your XML:

<?xml version="1.0"?>
<!DOCTYPE doc [
  <!ENTITY auth "Francesco">
]>
<doc>
  <string name="author">&auth;</string>
  <string name="about_application">Author: &auth;</string>
</doc>

When read by an XML parser, the document will be parsed and evaluated, or "seen", as:

<?xml version="1.0"?>
<doc>
  <string name="author">Francesco</string>
  <string name="about_application">Author: Francesco</string>
</doc>
like image 134
Mads Hansen Avatar answered Sep 28 '22 00:09

Mads Hansen


Unfortunately I don't think that is possible. I asked a similar question a while ago, and was told it wasn't possible.

like image 35
Cheryl Simon Avatar answered Sep 28 '22 01:09

Cheryl Simon