Search code examples
androidxmlandroid-studiokotlinandroid-viewbinding

Creating Navigation Drawer using View Binding


I'm trying to make a Navigation Drawer using View Binding and for some reason, when I create the properties of the binding variable (in the kotlin file) corresponding to the views in my layout file, it gives me an unresolved reference error.

Here is the MainActivity kotlin code:

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setUpToolbar()

        val actionBarDrawerToggle = ActionBarDrawerToggle(          
            this@MainActivity,
            binding.root,
            R.string.open_drawer,
            R.string.close_drawer)
        binding.root.addDrawerListener(actionBarDrawerToggle)       
        actionBarDrawerToggle.syncState()

        binding.navigationView.setNavigationItemSelectedListener {

            when(it.itemId){
                R.id.dashboard -> {
                    Toast.makeText (
                        this@MainActivity,
                        "Clicked on Dashboard",
                        Toast.LENGTH_LONG
                    ).show()
                }
                R.id.favourites -> {
                    Toast.makeText (
                        this@MainActivity,
                        "Clicked on favourites",
                        Toast.LENGTH_LONG
                    ).show()
                }
                R.id.profile -> {
                    Toast.makeText (
                        this@MainActivity,
                        "Clicked on profile",
                        Toast.LENGTH_LONG
                    ).show()
                }
                R.id.aboutApp -> {
                    Toast.makeText (
                        this@MainActivity,
                        "Clicked on about",
                        Toast.LENGTH_LONG
                    ).show()
                }
            }

            return@setNavigationItemSelectedListener true
        }


    }

    fun setUpToolbar(){
        supportActionBar?.title = "Toolbar Title"               
        supportActionBar?.setHomeButtonEnabled(true)            
        supportActionBar?.setDisplayHomeAsUpEnabled(true)      
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {     
        val id = item.itemId                                            
        if(id == android.R.id.home){                                    
            binding.root.openDrawer(GravityCompat.START)               
        }
        return super.onOptionsItemSelected(item)
    }
}

this is my xml code:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout"
    tools:context=".MainActivity"
    tools:viewBindingIgnore="true" >

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/design_default_color_primary_dark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"
        android:layout_gravity = "start"/>

</androidx.drawerlayout.widget.DrawerLayout>

My main issue is with the binding.navigationView.setNavigationItemSelectedListener line in the onCreate() method. the navigationView part comes up in red and when I run the code it shows me an "unresolved reference" error on that line. I don't understand why that's happening. Is there some variable I need to declare before directly writing that code?

Also, I've used binding.root in various places as a replacement for binding.drawerLayout because if I use binding.drawerLayout in, say the ActionBarDrawerToggle parameters, it again shows me an unresolved reference error. But when I use binding.root it works fine. But all of the tutorials I've seen online don't use this method so even though I could swerve around this problem, I'm not able to get to the root of it.

I tried invalidating caches, cleaning and rebuilding the project but that didn't work. It'd be great if someone could point out where I'm going wrong


Solution

  • Issue is with attribute tools:viewBindingIgnore="true" used on root tag (DrawerLayout in this case).

    According to documentation for View Binding

    If you want a layout file to be ignored while generating binding classes, add the tools:viewBindingIgnore="true" attribute to the root view of that layout file.

    In this case, O.P. wanted to generate binding classes but view binding was ignoring that due to this attribute hence the error.