Search code examples
androiddata-binding

Databinding remove html tags from string


I want to use simply tags from xml resourced String in databing.

public class StringUtils {
    public static String text(String a) {
        return a;
    }
}

XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{StringUtils.text(@string/underlined_text)}" />

String :

    <string name="underlined_text">This is a <u>underlined</u> text.</string>

In the end when i debugging text method i realized that < u > tag is removed.


Solution

  • You could try and use HTML escape codes:

    <string name="underlined_text">This is a &lt;u&gt;underlined&lt;/u&gt; text.</string>

    I'd also question whether databinding is really required here - you can just use android:text="@string/underlined_text"

    Edit: Also came across this answer which could be of use to you