40

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?

buczek
  • 1,989
  • 7
  • 28
  • 39
mudit
  • 24,821
  • 31
  • 86
  • 132

2 Answers2

103

%% 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 %"

ban-geoengineering
  • 17,070
  • 21
  • 157
  • 242
  • 1
    Worked for me in `%1$d per %2$s (1 per %3$s days) Accuracy=%4$s%%` 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)));` – MikeT Feb 25 '17 at 08:36
  • 3
    In fact this one works, but `%` doesn't in case if people use string format – deathangel908 Mar 23 '17 at 13:40
  • Thanks so much,it worked for me,% deosn't work in follow way: %1$d % getString(R.string.num_percent,10),this will crash. – aolphn Jun 22 '18 at 08:53
  • this should be the excepted answer since it will work for all cases unlike the accepted answer. – mpellegr Nov 30 '18 at 15:01
  • It works fine in all cases, even with string format, where, unfortunately, the accepted answer doesn't work. This should be the accepted answer. – Mattia Ruggiero Dec 11 '20 at 13:41
53

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

Also check the similar question: Android XML Percent Symbol

Community
  • 1
  • 1
olshevski
  • 4,846
  • 1
  • 34
  • 34
  • Thanks it worked.. i have to made one more adjustment which i found via the link you provided. – mudit Oct 22 '12 at 12:53
  • Thanks for link that is actually needed. – Shridutt Kothari Dec 29 '16 at 03:40
  • 1
    Did not work : `java.util.IllegalFormatConversionException: %e can't format java.lang.Integer arguments` – woprandi Oct 17 '18 at 08:20
  • 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. – mpellegr Nov 30 '18 at 14:59