9

In my android app, I have a large string resource xml file. I want to make reference and reuse declared resources values within String values. Is it possible to have the R class resolve referenced values (a la @string/db_table_name)?

<resources>
<string name="db_table_name">tbl_name</string>
<string name="ddl">create table @string/tbl_name</string>
</resources>

Is there a way of doing this. In regular Java world, some tools use ${varname} expression to resolve reference. Can this be done at all in Android?

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
vladimir.vivien
  • 512
  • 1
  • 6
  • 8
  • 2
    you probably dont need your db naming and query creation stuff in xml resources and they will not be dependent on localisation or device qualifiers - I would vote for having this stuff in a global project config file and maybe a static class for query creation - just a thought! Im not aware the above is possible is res files :) – Dori Jul 13 '11 at 13:43

4 Answers4

12

Add a %s to your second resource string (the one that you want to be dynamic) where you want it to be modified. i.e.,

<resources>
<string name="db_table_name">tbl_name</string>
<string name="ddl">create table %s</string>
</resources>

and in your code use getString() to work the magic,

getString(R.string.ddl, getString(R.string.db_table_name));
source.rar
  • 8,002
  • 10
  • 50
  • 82
  • I am aware of this approach. I was wondering if the Android tooling would do this when R.java is generated. Using the proposed answer still requires me to do all of the work. – vladimir.vivien Jul 13 '11 at 18:17
5

It's indeed possible.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
  <!ENTITY appname "MyAppName">
  <!ENTITY author "MrGreen">
]>

<resources>
    <string name="app_name">&appname;</string>
    <string name="description">The &appname; app was created by &author;</string>
</resources>

You can even define your entity globaly e.g:

res/raw/entities.ent:

  <!ENTITY appname "MyAppName">
  <!ENTITY author "MrGreen">

res/values/string.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
    <!ENTITY % ents SYSTEM "./res/raw/entities.ent">
    %ents;   
]>

<resources>
    <string name="app_name">&appname;</string>
    <string name="description">The &appname; app was created by &author;</string>
</resources>
Joseph Garrone
  • 1,662
  • 19
  • 20
  • 1
    Hi, I tried this and got "Unresolved Entity Reference" my path is "/raw/entities/ent" because when I set the path to be the same as it written in the example, everything is marked in red and I get "can't find res" – Elior Apr 24 '18 at 12:34
  • 2
    I got : Error: The entity "appname" was referenced, but not declared. I did exactly like the exemple, but could get it working... – maudem Jun 18 '18 at 22:31
  • This solution actually does not work for me: see my question: https://stackoverflow.com/questions/50933985/trying-to-use-entity-in-android-resources-with-error-the-entity-was-reference – Seraphim's Jun 20 '18 at 13:12
  • @maudem Hi friend! If you found a solution please tell me! :) – Seraphim's Jun 20 '18 at 13:12
  • 1
    Are the error you got reported by Android studio? It is possible that Android studio does not support external entities I do not use Android studio myself.... Maybe there is a way to tell Android studio to ignore the "...was referenced, but not declared" error and build anyway. If someone manage to do so report it and I will update my answer. – Joseph Garrone Jun 20 '18 at 14:36
  • @JosephGarrone I think the problem conerns Gradle – Seraphim's Jun 29 '18 at 14:50
  • This feature was removed from Android Studio. – ThomasW Feb 12 '19 at 04:30
1

Yes, it is possible without writing any Java/Kotlin code, only XML, by using this small library I created which does so at buildtime: https://github.com/LikeTheSalad/android-stem

Usage

Based on your example, you'd have to set your strings like this:

<resources>
  <string name="db_table_name">tbl_name</string>
  <string name="ddl">create table ${db_table_name}</string>
</resources>

And then, after building your project, you'll get:

<!-- Auto generated during compilation -->
<resources>
  <string name="ddl">create table tbl_name</string>
</resources>
César Muñoz
  • 535
  • 1
  • 6
  • 10
0

Well, I don't think this is possible. Because once the resources are allocated android won't allow us to change them dynamically in the air. Instead you can try having your Strings in a separate class and change them as you run through your code.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 1
    I am not interested in changing the string at runtime, but rather having the code generation of Android that creates the R.java class do the substitution for me at design time. Apparently it is not possible. – vladimir.vivien Jul 13 '11 at 19:38