androidxmlstringdata-bindingformatted

Binding formatted strings from XML file with data binding not working correctly


When developing with data binding to bind formatted strings directly in the XML file, the @{@string/stringName} does not work as normal attribute @string/stringName without data binding, and neither does @{String.format(@string/stringName)}.

I would like to know how to use data binding to use formatted strings such as <string name="my_string"> <font face="font_name" color="hex_color"> my text here with %s </font> </string> along with data binding, because @{@string/my_string("My name")} doesn't work correctly. I mean, it does appear with the "My name" inserted on the string, but no formatted attribute like font or color.

If I don't use data binding and just @string/myName the formatted attributes font and color work propperly, but I don't get the input for %s.

Thank you all.


Solution

  • For anyone still looking for this, you just have to call Span.fromHtml() in the XML file. Also don't forget to import it.

    @{Span.fromHtml(@string/stringName("Julio"))}