Viewbinding Android

Hello coders,

In android Apprentice, when you making a nav drawer for placebook. The author used android kotlin extension. However I t is now deprecated in favor of viewbinding. The problem is with the Viewbinding I am able to get the toolbar working with the main view xml.
However with the Viewbinding Setcontentview(R.layout.activity_maps) is not working. Therefore you have to set the activity_map to binding. However after I finish setting it and pass the root to the setcontentview. I still get an error when I compile and run.

Any help will be highly appreciated. Thanks

I’m in the process of updating that part of the book. What I did was add an id to the included layouts. This allows you to reference the included layouts with something like databinding.navlayout.textview

1 Like

Thank you @kevindmoore.

Hello sir it is not that part sorry. I am referring to page 384 drawerLayout.With the Kotlin android extension all I need to do is import main_view_ map.Xml as that will activate the toolbar. I am able to do that with Viewbinding without any issue

With kotlin android extension
Setcontentview will look like this:
setContentView(R. layout.activity.maps).

If I use viewbinding for this part I get a runtime error.
Sir Look at this picture please
16123418878765179429361769318665

Thank you sir.

I don’t think you want to have 2 different Binding classes. This is what I have:
databinding = ActivityMapsBinding.inflate(layoutInflater)
setContentView(databinding.root)

And this is my XML:

  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">
  <include android:id="@+id/mainMapView" layout="@layout/main_view_maps"/>
  <include android:id="@+id/drawerViewMaps" layout="@layout/drawer_view_maps"/>
</androidx.drawerlayout.widget.DrawerLayout>```
1 Like

What about the toolbar sir?

private fun setupToolbar() {
setSupportActionBar (ActivityMapsBinding.inflate(layoutInflater. toolbar)
}

Sir this does not work for me.

However using the MainViewMapsBinding.inflate(layoutInflater) does activate the toolbar but I get a runtime error. Whenever I run the app.

    setSupportActionBar(databinding.mainMapView.toolbar)
1 Like

Thank you sir it is working now. I can’t wait to get the fourth edition.
I hope it arrives soon.

This topic was automatically closed after 166 days. New replies are no longer allowed.