# Viewbinding Android

**URL:** <https://forums.kodeco.com/t/viewbinding-android/131747>\
**Category:** Android\
**Created:** [February 3, 2021, 12:01am UTC](https://forums.kodeco.com/t/viewbinding-android/131747 "2021-02-03T00:01:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![doer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/doer/32/105896_2.png) [@doer](https://forums.kodeco.com/u/doer)\
**Post date:** [February 3, 2021, 12:01am UTC](https://forums.kodeco.com/t/viewbinding-android/131747/1 "2021-02-03T00:01:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevindmoore](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kevindmoore/32/109046_2.png) [@kevindmoore](https://forums.kodeco.com/u/kevindmoore)\
**Post date:** [February 3, 2021, 3:18am UTC](https://forums.kodeco.com/t/viewbinding-android/131747/2 "2021-02-03T03:18:15Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![doer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/doer/32/105896_2.png) [@doer](https://forums.kodeco.com/u/doer)\
**Post date:** [February 3, 2021, 3:28am UTC](https://forums.kodeco.com/t/viewbinding-android/131747/3 "2021-02-03T03:28:23Z")

</div>

Thank you @kevindmoore.

---

<div class="post-metadata">

**Author:** ![doer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/doer/32/105896_2.png) [@doer](https://forums.kodeco.com/u/doer)\
**Post date:** [February 3, 2021, 8:47am UTC](https://forums.kodeco.com/t/viewbinding-android/131747/4 "2021-02-03T08:47:33Z")

</div>

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](https://files.chunter.kodeco.com/original/3X/3/c/3c2d8d7f2a38a3dd8aee31bb6ce478250e32557d.jpeg)

Thank you sir.

---

<div class="post-metadata">

**Author:** ![kevindmoore](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kevindmoore/32/109046_2.png) [@kevindmoore](https://forums.kodeco.com/u/kevindmoore)\
**Post date:** [February 3, 2021, 4:10pm UTC](https://forums.kodeco.com/t/viewbinding-android/131747/5 "2021-02-03T16:10:12Z")

</div>

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:

````auto
  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>```
````

---

<div class="post-metadata">

**Author:** ![doer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/doer/32/105896_2.png) [@doer](https://forums.kodeco.com/u/doer)\
**Post date:** [February 3, 2021, 4:33pm UTC](https://forums.kodeco.com/t/viewbinding-android/131747/6 "2021-02-03T16:33:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevindmoore](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kevindmoore/32/109046_2.png) [@kevindmoore](https://forums.kodeco.com/u/kevindmoore)\
**Post date:** [February 3, 2021, 4:47pm UTC](https://forums.kodeco.com/t/viewbinding-android/131747/7 "2021-02-03T16:47:44Z")

</div>

```auto
    setSupportActionBar(databinding.mainMapView.toolbar)

```

---

<div class="post-metadata">

**Author:** ![doer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/doer/32/105896_2.png) [@doer](https://forums.kodeco.com/u/doer)\
**Post date:** [February 3, 2021, 5:04pm UTC](https://forums.kodeco.com/t/viewbinding-android/131747/8 "2021-02-03T17:04:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [July 19, 2021, 4:01pm UTC](https://forums.kodeco.com/t/viewbinding-android/131747/9 "2021-07-19T16:01:10Z")

</div>

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