0

I've made a DrawerLayout:

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/activity_main"/>

    <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"/>

</androidx.drawerlayout.widget.DrawerLayout>

What worked out of the box was to slide in the drawer from the left side. To get the hamburger icon I added this code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.navigation_drawer)

    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    val drawerLayout = findViewById<DrawerLayout>(R.id.drawer_layout)
    val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, 0, 0)
    drawerLayout.addDrawerListener(drawerToggle)
    drawerToggle.syncState()
}

That gives me the hamburger icon along with a nice animation when opening/closing the drawer. What still doesn't work is the click on the hamburger icon. It does nothing. I would like it to open/close the drawer. What am I missing?

As a side question: what is the purpose of the resource string ids for ActionBarDrawerToggle? I've just set them to 0. Is that ok?

user1785730
  • 3,150
  • 4
  • 27
  • 50
  • 1
    Have a look here: https://stackoverflow.com/a/36570774. As for the resource IDs, they're for the content description for the toggle button when the drawer is in the corresponding open or closed state. The content description is used for things like Accessibility, Talk Back, etc. Technically, `0` is an invalid resource ID, but if it didn't crash right away, it apparently checks before trying to look it up blindly. – Mike M. Mar 05 '22 at 17:34
  • 1
    Thanks, the linked answer provides the solution. – user1785730 Mar 05 '22 at 17:53

0 Answers0