Android: Can't toggle visibility after migrating from kotlin synthetic to databinding

Viewed 529

As kotlin synthetic got deprecated (like every stuff gets in android for no reason), I am currently working to fully migrate to databinding. A problem I am now encountering is, that I for example can't toggle the visibility of an imported layout, it simply says

Unresolved reference: visibility

And this makes even sense, because the variable I am getting from my layout (calibrateRepairBtnView) is another type of binding which has no function visibility.

My question is: How can I solve this WITHOUT writing 10 lines of code, where I toggle the visibility of every item inside my calibrateRepairBtnView? Am I missing something here?

Fragment

@AndroidEntryPoint
class CalibrateRepairMessageFragment : BaseCalibrateRepairFragment<FragmentCalibrateRepairMessageBinding>(
    R.layout.fragment_calibrate_repair_message,
) {
    private val calibrateRepairMessageBinding: FragmentCalibrateRepairMessageBinding by viewBinding()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    private fun sucessState() {
        calibrateRepairMessageBinding.calibrateRepairBtnView.visibility // not working anymore
        calibrate_repair_btn_view.visibility = View.VISIBLE // worked before
    }

Layout (imported)

<?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="price"
            type="Float" />
        <variable
            name="btnText"
            type="String" />
        <variable
            name="btnIcon"
            type="android.graphics.drawable.Drawable" />
        <variable
            name="btnTextCapitalized"
            type="Boolean" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/calibrate_btn_background"
            android:layout_width="match_parent"
            android:layout_height="124dp"
            android:background="@color/color_btn_gray"
            android:clickable="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
        </com.google.android.material.appbar.MaterialToolbar>

        <TextView
            android:id="@+id/calibrate_btn_tv_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/standard8dpMargin"
            android:layout_marginEnd="@dimen/big16dpMargin"
            android:text="@string/calibrate_btn_price"
            android:textColor="@color/color_text_dark"
            android:textSize="@dimen/textHeadlineNormal2"
            app:layout_constraintEnd_toStartOf="@+id/calibrate_btn_tv_price"
            app:layout_constraintTop_toTopOf="@+id/calibrate_btn_background" />

        <TextView
            android:id="@+id/calibrate_btn_tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{price}"
            android:textColor="@color/color_text_blue"
            android:textSize="@dimen/textHeadlineNormal2"
            android:textStyle="bold"
            tools:text="4800.00"
            app:layout_constraintBottom_toBottomOf="@+id/calibrate_btn_tv_sum"
            app:layout_constraintEnd_toStartOf="@+id/calibbrate_btn_tv_intern_curr"
            app:layout_constraintTop_toTopOf="@+id/calibrate_btn_tv_sum" />

        <TextView
            android:id="@+id/calibbrate_btn_tv_intern_curr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/currency"
            android:textColor="@color/color_text_blue"
            android:textSize="@dimen/textHeadlineNormal2"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/calibrate_btn_tv_price"
            app:layout_constraintEnd_toEndOf="@+id/calibrate_btn_next"
            app:layout_constraintTop_toTopOf="@+id/calibrate_btn_tv_price" />

        <TextView
            android:id="@+id/calibrate_btn_tv_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="@string/calibrate_btn_desc"
            android:textColor="@color/color_text_gray"
            android:textSize="@dimen/textDescriptionNormal4"
            app:layout_constraintEnd_toEndOf="@+id/calibrate_btn_next"
            app:layout_constraintTop_toBottomOf="@+id/calibrate_btn_tv_price" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/calibrate_btn_next"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_marginStart="@dimen/big16dpMargin"
            android:layout_marginEnd="@dimen/big16dpMargin"
            android:layout_marginBottom="@dimen/standard8dpMargin"
            android:clickable="true"
            android:focusable="true"
            android:text="@{btnText}"
            android:textAllCaps="@{btnTextCapitalized}"
            android:textSize="@dimen/textDescriptionNormal1"
            android:textStyle="normal"
            app:backgroundTint="@color/color_btn_blue"
            app:icon="@{btnIcon}"
            app:iconGravity="textStart"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/calibrate_btn_background" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Layout (fragment)

<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="com.kofigyan.stateprogressbar.StateProgressBar.StateNumber" />

        <variable
            name="viewModel"
            type="com.example.app.framework.ui.viewmodel.EmailViewModel" />
        <variable
            name="outsideTT"
            type="String" />
    </data>

       <!-- USING THE LAYOUT HERE (binding.calibrateRepairBtnView.visibility) -->
        <include
            android:id="@+id/calibrate_repair_btn_view"
            layout="@layout/app_standard_checkout_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:price="@{viewModel.price}"
            app:btnText="@{@string/calibrate_btn_text}"
            app:btnTextCapitalized="@{true}" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2 Answers

Okay, I've managed to solve this problem. Instead of doing calibrateRepairMessageBinding.calibrateRepairBtnView.visibility I did calibrateRepairMessageBinding.calibrateRepairBtnView.root.visibility. As roots says

Returns the outermost View in the layout file associated with the Binding. If this binding is for a merge layout file, this will return the first root in the merge tag.

With this, the root object (should) return the outermost view, which is the constraintlayout associated with the view. Toggling the constrainslayouts view would toggle the entire view, therefore this suited my case.

If someone has a better answer or thinks that this might be wrong, feel free to leave a comment or provide a better answer.

You can try this

android:visibility="@{viewModel.boolean? View.VISIBLE : View.GONE, default=gone}"

here I've just added boolean so that you can define your own condition

Related