Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I concatenate static strings with XML string resources?

I'm trying to combine a static "hard coded" string with one referenced from strings.xml for string array items.

The goal is to have a dynamic metrics list where the number is the same for all languages, but the metrics text value may change by language, something like this:

<string-array name="interval_labels">
    <item>30 @string/second</item>
    <item>1 @string/minute</item>
    <item>5 @string/minute</item>
    <item>10 @string/minute</item>
    <item>15 @string/minute</item>
    <item>30 @string/minute</item>
    <item>60 @string/minute</item>
</string-array>

Right now, if I remove the numbers before the @string/... references, it works well (as mentioned here), but I was wondering whether there is a way to retrieve the referenced string and concatenate it to the "hard coded" one.

like image 927
Tõnis Pool Avatar asked Nov 20 '11 18:11

Tõnis Pool


People also ask

What is concat in XML?

The XMLConcat() function concatenates two XML objects (either two elements or two attributes) to produce a single XML object.

What is the advantage of making strings in strings XML file?

One of the main benefits is for localization: you keep your code language-independent and just need to provide a different XML file for each language you want to support.

What is the purpose of strings XML file inside resource directory?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.


2 Answers

Sorry, no such syntax is supported by Android resource files.

like image 104
CommonsWare Avatar answered Nov 01 '22 11:11

CommonsWare


Using XML entities it's possible.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
  <!ENTITY mintues "minutes">
  <!ENTITY minute "minute">
  <!ENTITY seconds "seconds">
]>

<resources>
  <string-array name="interval_labels">
    <item>30 &seconds;</item>
    <item>1 &minute;/item>
    <item>5 &minutes;</item>
    <item>10 &minutes;</item>
    <item>15 &minutes;</item>
    <item>30 &minutes;</item>
    <item>60 &minutes;</item>
  </string-array>
</resources>

I used this answer: dynamic String using String.xml?

like image 22
Andrew Avatar answered Nov 01 '22 12:11

Andrew