Skip to content
Advertisement

How to set visibility for include layout in databinding?

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);

Advertisement

Answer

add get root to your view

dataBinding.regPhone.getRoot().setVisibility(View.GONE);
dataBinding.regEmail.getRoot().setVisibility(View.VISIBLE);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement