How to set visibility for include layout in databinding?

0

Issue

I have implemented data binding in my project. I have a particular screen with two nested layouts in include tags. I couldn’t change the visibility for include layouts using data binding programmatically.

However, I have achieved it through a boolean, but my question is how to set visibility for that include tag programmatically.

My xml:

<include
  android:id="@+id/reg_email"
  layout="@layout/custom_email"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>


<include
  android:id="@+id/reg_phone"
  layout="@layout/custom_phone"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

And in Activity:
when I try to set this – it becomes red meaning it doesn’t take it as a view.

  dataBinding.regPhone.setVisibility(View.GONE);
  dataBinding.regEmail.setVisibility(View.VISIBLE);

Solution

add get root to your view

dataBinding.regPhone.getRoot().setVisibility(View.GONE);
dataBinding.regEmail.getRoot().setVisibility(View.VISIBLE);

Answered By – mahdi shahbazi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More