22

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!

2 Answers 2

26

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

Sign up to request clarification or add additional context in comments.

2 Comments

ps.: I wrote all this by heart, there might be some typo.
Wish I could accept both your answers. What it actually worked out was <string name="my_name">Text here num %1$d and num %2$d </string> and your getString(R.string.my_name), getResources().getInteger(R.integer.num1), getResources().getInteger(R.integer.num2));
7

Others said your approach do not work. This is an old answer to face similar problem:

<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>

int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage);
String strMeatMsg = String.format(strMeatFormat, numPoundsMeat);

See this references:

1 Comment

Mix my answer with provided by @Budius

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.