title: Dynamically toggle visibility of layout elements with Android data-binding.
body: <p>I am building an Android app, where I want to toggle the visibility of some view elements based on a button click. I am trying to archive it with data-binding instead of using findViewById(), but all the solutions I've have found until now doesn't update the layout, when the variable is changed.</p>
  
  <p>Here is what I have so far.  (I've simplified the code, to focus on the problem)</p>
  
  <p>Activicy.java</p>
  
  <pre><code>public class RecipeActivity extends AppCompatActivity {
  private Recipe recipe;
  private ActivityRecipeBinding binding;
  private RecipeBinderHelper rbhelper = new RecipeBinderHelper();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Intent intent = getIntent();
      recipe = intent.getParcelableExtra("recipe");
      binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      toolbar.setTitle(recipe.getName());
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
      binding.recipeContent.setRecipe(recipe);
      binding.recipeContent.setHelper(rbhelper);
  
      binding.Button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              //HERE I CHANGE THE VALUE OF THE VARIBLE
              rbhelper.setPresentationViewVisible(false);
              binding.notifyChange();
          }
      });
  }
  }
  </code></pre>
  
  <p>Helper class</p>
  
  <pre><code>public class RecipeBinderHelper{
  private Boolean presentationElementsVisible;
  private Boolean timerElementsVisible;
  
  public RecipeBinderHelper(){
      this.presentationElementsVisible = true;
      this.timerElementsVisible = false;
  }
  public void setPresentationViewVisible(boolean presentationElementsVisible) {
      this.presentationElementsVisible = presentationElementsVisible;
  }
  public Boolean getPresentationElementsVisible() {
      return presentationElementsVisible;
  }
  //getters and setters for private Boolean timerElementsVisible;
  }
  </code></pre>
  
  <p>Layout</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  &lt;data&gt;
      &lt;import type="android.view.View"/&gt;
      &lt;variable
          name="helper"
          type="com.myapps.recipeApp.RecipeBinderHelper"/&gt;
      &lt;variable
          name="recipe"
          type="com.myapps.recipeApp.Recipe"/&gt;
  &lt;/data&gt;
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.myapp.recipeApp.RecipeActivity"
      tools:showIn="@layout/activity_recipe"&gt;
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/r_source"
          android:textStyle="bold"
          android:text="@{recipe.source}"
          android:visibility="@{helper.presentationElementsVisible ? View.VISIBLE : View.GONE}" /&gt;
  &lt;!-- More TextViews here --&gt;
  &lt;!-- Button is located in parret layout --&gt;
  &lt;/RelativeLayout&gt;
  &lt;/layout&gt;
  </code></pre>