[android] Android XML Percent Symbol

I have an array of strings in which the % symbol is used. Proper format for using the % is %. When I have a string in that array with multiple % it gives me this 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

This question is related to android xml android-resources xliff

The answer is


You can escape % using %% for XML parser, but it is shown twice in device.

To show it once, try following format: \%%

For Example

<string name="zone_50">Fat Burning (50\%% to 60\%%)</string> 

is shown as Fat Burning (50% to 60%) in device


This could be a case of the IDE becoming too strict.

The idea is sound, in general you should specify the order of substitution variables so that should you add resources for another language, your java code will not need to be changed. However there are two issues with this:

Firstly, a string such as:

You will need %.5G %s

to be used as You will need 2.1200 mg will have the order the same in any language as that amount of mass is always represented in that order scientifically.

The second is that if you put the order of variables in what ever language your default resources are specified in (eg English) then you only need to specify the positions in the resource strings for languages the use a different order to your default language.

The good news is that this is simple to fix. Even though there is no need to specify the positions, and the IDE is being overly strict, just specify them anyway. For the example above use:

You will need %1$.5G %2$s


In your strings.xml file you can use any Unicode sign you want.

For example, the Unicode number for percent sign is 0025:

<string name="percent_sign">&#x0025;</string>

You can see a comprehensive list of Unicode signs here


Try this one (right):

<string name="content" formatted="false">Great application %s  ?  %s  ?  %s \\n\\nGo to download this application %s</string>

You can escape the % in xml with %%, but you need to set the text in code, not in layout xml.


Try using a backslash in front of it, like below:

\%

To allow the app using formatted strings from resources you should correct your xml. So, for example

<string name="app_name">Your App name, ver.%d</string>

should be replaced with

<string name="app_name">Your App name, ver.%1$d</string>

You can see this for details.


Suppose you want to show (50% OFF) and enter 50 at runtime. Here is the code:

<string name="format_discount"> (
<xliff:g id="discount">%1$s</xliff:g>
<xliff:g id="percentage_sign">%2$s</xliff:g>
 OFF)</string>

In the java class use this code:

String formattedString=String.format(context.getString(R.string.format_discount),discountString,"%");
holder1.mTextViewDiscount.setText(formattedString);

Per google official documentation, use %1$s and %2$s http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Hello, %1$s! You have %2$d new messages.


A tricky method: using small percent sign as below

 <string name="zone_50">Fat Burning (50&#65130; to 60&#65130;)</string> 

Percent Sign on Wikipedia


Use

<string name="win_percentage">%d%% wins</string>

to get

80% wins as a formatted string.

I'm using String.format() method to get the number inserted instead of %d.


Not exactly your problem but a similar one.

If you have more than one formatting in your string entry, you should not use "%s" multiple times.

DON'T :

<string name="entry">Planned time %s - %s (%s)</string>

DO :

<string name="entry">Planned time %1$s - %2$s (%3$s)</string>

to escape the percent symbol, you just need %%

for example :

String.format("%1$d%%", 10)

returns "10%"


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to android-resources

Failed linking file resources Execution failed for task ':app:processDebugResources' even with latest build tools getResources().getColor() is deprecated getColor(int id) deprecated on Android 6.0 Marshmallow (API 23) Android getResources().getDrawable() deprecated API 22 Android Studio cannot resolve R in imported project? Android XXHDPI resources Android ImageView setImageResource in code format statement in a string resource file Lint: How to ignore "<key> is not translated in <language>" errors?

Examples related to xliff

Android XML Percent Symbol