HTML formatting in Android's new data binding library
You must import Html and then call the fromHtml method :
<data>
<import type="android.text.Html"/>
</data>
…
<TextView
android:text="@{Html.fromHtml(@string/my_html)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In case if you want to use string with html tags and combine it with string parameters, it could be done in a such way:
In layout:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{activity.formattedString}" />
In your activity (for ex):
public CharSequence getFormattedString() {
if(selectedItem == null) return null;
String str = String.format(Html.toHtml(SpannedString.valueOf(this.getResources().getText(R.string.your_tagged_string))), parameter);
return Html.fromHtml(str);
}
Transforms should not be done in the view. The viewmodel is here to do transform operations between your raw model and your view.
So I would rather do it like that :
<data>
<variable
name="viewModel"
type="yourpackage.YourViewModel"/>
</data>
…
<TextView
android:text="@{viewModel.htmlText}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In your viewmodel :
private Model model; // your model
public Spanned getHtmlText(){
return Html.fromHtml(model.htmlText);
}