Search code examples
androidandroid-viewmodelandroid-binder

User Data Binding to Bind Html. fromHtml String android


hi i am new in mvvm structure and i am learning new things over here. Currently i have bind default string with my view i have multi color text so i choose to use HTML.fomHTml nut i am unable to use can anyone help to solve this binding issue below my testing code ....

data>

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

    <variable
        name="loginviewmodel"
        type="app.road2xtech.neighbourhood.view.viewmodel.LoginViewModel" />

</data>



  <TextView
        android:id="@+id/textViewAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="56dp"
        android:fontFamily="@font/raleway_regular"
        android:gravity="center"
        android:text="@={Html.fromHtml(loginviewmodel.accountString)}"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

it will give me below error

The expression 'android.text.Html.fromHtml(loginviewmodelAccountString)' cannot be inverted, so it cannot be used in a two-way binding

Details: There is no inverse for method fromHtml, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions

Solution

  • This is called two-way data binding.

    android:text="@={Html.fromHtml(loginviewmodel.accountString)}"
    

    The InverseMethod annotation may be applied to any method used in two-way data binding. So to sum up in your XMl if you don't need two-way binding then kindly use this.

     android:text="@{Html.fromHtml(loginviewmodel.accountString)}"
    

    This will work. :)