42

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
  • 2,011
  • 7
  • 29
  • 40
mudit
  • 25,306
  • 32
  • 90
  • 132

3 Answers3

108

%% 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
  • 18,324
  • 27
  • 171
  • 253
  • 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
54

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

Also check the similar question: Android XML Percent Symbol

Community
  • 1
  • 1
olshevski
  • 4,971
  • 2
  • 35
  • 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
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

tanni tanna
  • 544
  • 4
  • 12