title: Using ViewBinding with multiple layout
body: <p><strong><em>Edit - solution</em></strong></p>
  
  <p>When using view binding, a Java binding file is generated for each xml. The problem was that when you modify the xml, it doesn't regenerate the Java files. So, when I added an ID tag to the App_bar_main in activity_main.xml, I was getting an error message saying it can't find that tag not because it doesn't like it but because it was still using the older Java files. Invalidating the caches and restart didn't affect those generated java binding files neither.</p>
  
  <p>The only way to regenerate the files was to delete them.</p>
  
  <p>With the added ID, I just had to call appBarMain using its ID instead of trying to inflate its layout. </p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends AppCompatActivity
          implements NavigationView.OnNavigationItemSelectedListener {
  ...
      private ActivityMainBinding mActivityMainBinding;
      private AppBarMainBinding mAppBarMainBinding;
  ...
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
          setContentView(mActivityMainBinding.getRoot());
  
          mAppBarMainBinding = mActivityMainBinding.appBarMain;
          setSupportActionBar(mAppBarMainBinding.toolbar);
  
          mAppBarMainBinding.fabCollapse.fab_collapse.setOnClickListener(view -&gt; {
              Log.d(TAG, "Clicked fab_collapse");
          });
     }
  }
  </code></pre>
  
  <p><strong><em>activity_main.xml</em></strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.drawerlayout.widget.DrawerLayout 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"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:openDrawer="start"&gt;
  
      &lt;include
          layout="@layout/app_bar_main"
          android:id="@+id/app_bar_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;com.google.android.material.navigation.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_main"
          app:menu="@menu/activity_main_drawer" /&gt;
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>Thank you all for your contributions </p>
  
  <p><strong><em>Original</em></strong></p>
  
  <p>I'm trying to switch the <code>findViewById</code> calls in my app to the newly added <code>ViewBinding</code> but I'm having issues interacting with the views other than the one called in <strong><em><code>setContentView()</code></em></strong>. </p>
  
  <p>Is the way I'm inflating <strong><em>AppBarMainBinding</em></strong> wrong? or am I missing something else?</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends AppCompatActivity
          implements NavigationView.OnNavigationItemSelectedListener {
  ...
      private ActivityMainBinding mActivityMainBinding;
      private AppBarMainBinding mAppBarMainBinding;
  ...
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
          setContentView(mActivityMainBinding.getRoot());
  
          mAppBarMainBinding = AppBarMainBinding.inflate(getLayoutInflater());
          setSupportActionBar(mAppBarMainBinding.toolbar);
  
          mAppBarMainBinding.fabCollapse.fab_collapse.setOnClickListener(view -&gt; {
              Log.d(TAG, "Clicked fab_collapse");
          });
     }
  }
  </code></pre>
  
  <p>The <code>activity_main.xml</code> contains <code>&lt;include layout="@layout/app_bar_main" /&gt;</code> and a menu</p>
  
  <p><code>app_bar_main.xml</code> contains a toolbar and some action buttons</p>
  
  <p>In this case, if i call <strong><em><code>setContentView(mActivityMainBinding.getRoot())</code></em></strong>, in the main activity's onCreat(), the app launches the <code>activity_main</code> layout and displays the action button and the toolbar placeholder but none of the buttons work. Clicking on the action button doesn't invoke its <code>setOnClickListener</code> and the toolbar is blank (it doesn't implement <code>setSupportActionBar(toolbar)</code>)</p>
  
  <p><strong><em>activity_main.xml</em></strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.drawerlayout.widget.DrawerLayout 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"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:openDrawer="start"&gt;
  
      &lt;include
          layout="@layout/app_bar_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;com.google.android.material.navigation.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_main"
          app:menu="@menu/activity_main_drawer" /&gt;
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  </code></pre>
  
  <p><strong><em>app_bar_main.xml</em></strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;!-- Min api = 21
          Add: Screen capture  --&gt;
  
  &lt;androidx.coordinatorlayout.widget.CoordinatorLayout 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"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"&gt;
  
  &lt;com.google.android.material.appbar.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
      &lt;androidx.appcompat.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
  &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
  &lt;include layout="@layout/content_main" /&gt;
  
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      android:id="@+id/fab_collapsed_layout"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|end"
      android:layout_margin="@dimen/fab_margin"
      app:layout_dodgeInsetEdges="bottom"&gt;
  
      &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab_collapse"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:backgroundTint="@android:color/holo_orange_dark"
          app:fabSize="mini"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:srcCompat="@android:drawable/arrow_up_float" /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>Thanks</p>