43

I want to create the following string array in my strings.xml file:

<string-array name="an_array_columns">
         <item>% Select percentage1</item>
         <item>% Select percentage2</item>
</string-array>

here the string is "% select percentage". Now, when I am writing this in my strings.xml, I am getting the following error:

Multiple annotations found at this line:
    - error: Multiple substitutions specified in non-positional format; did you mean to 
     add the formatted="false" attribute?
    - error: Found tag </item> where </string-array> is expected

I know we can change & to &amp; or ' to \', but what do I do for percentage sign?

3 Answers 3

115

%% seems to be all that works for me. For example:

<string name="num_percent">%1$d %%</string>

with

String oneHundredPercentString = getString(R.string.num_percent, 100); // This gives "100 %"

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

6 Comments

Worked for me in <string name="rulestats">%1$d per %2$s (1 per %3$s days) Accuracy=%4$s%%</string> with currentrule.setText(ctxt.getResources().getString( R.string.rulestats, csr.getInt(csr.getColumnIndex( DBRulesTableConstants.RULES_USES_COL)), RulePeriodAsString(ruleperiodasint,rulemultiplierasint), df.format(rulequantityperday), df.format(ruleaccuracy)));
In fact this one works, but &#37; doesn't in case if people use string format
Thanks so much,it worked for me,&#37; deosn't work in follow way: <string name="num_percent">%1$d &#37;</string> getString(R.string.num_percent,10),this will crash.
this should be the excepted answer since it will work for all cases unlike the accepted answer.
It works fine in all cases, even with string format, where, unfortunately, the accepted answer doesn't work. This should be the accepted answer.
|
54

You can use its code. Try this: &#37;

Also check the similar question: Android XML Percent Symbol

2 Comments

Did not work : java.util.IllegalFormatConversionException: %e can't format java.lang.Integer arguments
got the same problem as woprandi. trying to add a percent at the end of a string formatted to allow integers. works without this added, but when I add the code above, the issue occurs.
0

If we want % symbol in string then try

<string name="_150_chance_for_a_beautiful_sky">150\% chance for a beautiful sky.</string>
binding.percentageCheck.setText(getResources().getString(R.string._150_chance_for_a_beautiful_sky));

If we want to format data then try

    <string name="test_percent">First : %d, Second : %s, third :%f, Second : %c  </string>

    String multiPercent = getResources().getString(R.string.test_percent, 150 , "Test", 2f, 'c');
    binding.multiPercentageCheck.setText(multiPercent);

Output

enter image description here

Comments

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.