Search code examples
androidmvvmandroid-architecture-components

MVVM BindingAdapters not showing ProgressBar


I'm trying to create a simple example with databinding and BindingAdapters in order to show/hide a ProgressBar depending on TextView if it's empty or not. Below you can see my code. What I'm doing wrong?

loading_state.xml

<?xml version="1.0" encoding="utf-8"?>
<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>
        <variable
            name="textString"
            type="String"/>
    </data>
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:visibleGone="@{textString==null}">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{textString}"/>
    </android.support.constraint.ConstraintLayout>
</layout>

My BindingAdapter

object BindingAdapters {
    @JvmStatic
    @BindingAdapter("visibleGone")
    fun showHide(view: View, visible: Boolean){
        view.visibility = if (visible) View.VISIBLE else View.GONE
    }
}

I include the layout in my second fragment in order to check the textview text

<include layout="@layout/loading_state"
    app:textString="@{textView2.text.toString()}"/>

and also in my SecondFramgent class I take the value from MainFragment class (I'm using the new Navigation component)

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    val txtFromMain = SecondFragmentArgs.fromBundle(arguments)
    textView2.text = txtFromMain.txtFromMain
}

What am I missing?

Thank you very much.


Solution

  • For those facing the same issue you can find my solutions matches in my case below:

    I had to change my BindingAdapter.

    @BindingAdapter("visibleGone")
    fun showHide(view: View, visible: String){
        view.visibility = if (visible.isEmpty()) View.VISIBLE else View.GONE
    }