Edit - solution
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.
The only way to regenerate the files was to delete them.
With the added ID, I just had to call appBarMain using its ID instead of trying to inflate its layout.
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 -> {
Log.d(TAG, "Clicked fab_collapse");
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<include
layout="@layout/app_bar_main"
android:id="@+id/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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" />
</androidx.drawerlayout.widget.DrawerLayout>
Thank you all for your contributions
Original
I'm trying to switch the findViewById
calls in my app to the newly added ViewBinding
but I'm having issues interacting with the views other than the one called in setContentView()
.
Is the way I'm inflating AppBarMainBinding wrong? or am I missing something else?
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 -> {
Log.d(TAG, "Clicked fab_collapse");
});
}
}
The activity_main.xml
contains <include layout="@layout/app_bar_main" />
and a menu
app_bar_main.xml
contains a toolbar and some action buttons
In this case, if i call setContentView(mActivityMainBinding.getRoot())
, in the main activity's onCreat(), the app launches the activity_main
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 setOnClickListener
and the toolbar is blank (it doesn't implement setSupportActionBar(toolbar)
)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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" />
</androidx.drawerlayout.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Min api = 21
Add: Screen capture -->
<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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<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" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<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">
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Thanks
You only need to set content view, then you can access appbar with mActivityMainBinding.appBarMain.