Search code examples
androidhtmldata-bindingandroid-databinding

Handle HTML entity with Data Binding


<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

    <data>
        <import type="android.text.Html"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:text="@{Html.fromHtml(`some message from model` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`)}"/>

    </RelativeLayout>

    </layout>

Here I am trying to leave some blank space after text in TextView, but it is showing this error.

Error:Execution failed for task ':app:dataBindingProcessLayoutsDebug'. 
The entity "nbsp" was referenced, but not declared.

I have tried with simple space in xml, but it doesn't leave blank spaces android:text="@{'some message from model' + ' '}"


Solution

  • Remove the @nbsp and try with it hexa(decimal) value &#160.

    because in XML the main five entity are below :

    quet("), amp(&), apos('), It(<) and gt(>) In which the @nbsp to not there so you need to use it hexa(decimal) value &#160.

    If We have to put Space in XML using HTMl we use &nbsp But in Xml it give this error :

    The entity "nbsp" was referenced, but not declared.

    Because the Main five entity of XMl is I define Upper so you have to use it hexa(decimal) value..

    so &nbsp you have to &#160. but again & is XMl Entity so you have convert & to &amp; so it will work.

    In SHORT :

    &nbsp means &amp;#160