Search code examples
androidxmlstringandroid-resources

Android string resource variable not appearing if between characters


This shows the integer fine <string name="count"> Count %d </string>

Example output: Count 3

however this does not, <string name="count"> Count ( %d )</string>

Example output: Count

I need to integer to be displayed between the parentheses, is this not achievable via the strings.xml file? I would rather not use a SpannableString and keep it simple with the string variable.

I'm using android Context class to get the string

@NonNull
public final String getString(@StringRes int resId, Object... formatArgs) {
    return getResources().getString(resId, formatArgs);
}

so... viewObject.text = context.getString(R.string.filter, integerCount)


Solution

  • Some characters in xml need to be escaped. You can use in your case :

    "&#40;" for ( 
    and 
    "&#41;" for )