Search code examples
javaandroidxmlandroid-resources

How to use integer resources within string resources in android


How can I use some integer that I have stored in my integers.xml file in my strings.xml file.

For example:

I have <integer name="some_integer">5</integer> and I would like to use this in my strings.xml file:

<string name="some_string">This is my string num @integers/some_integer in a row</string>

Apparently my way is not good enough so I need a little help please. I belive there is a possible solutions I just don't know the right one.

Appreciate all the help!


Solution

  • short version is that you can't mix resources like this, but you can use in Java:

    getResources.getString(R.string.some_string,
                           getResources.getInteger(R.integer.some_integer)
                          );
    

    and then in your String XML

    <string name="some_string">This is my string num %d in a row</string>
    

    that way the %d get replaced by the integer you pass in the getString