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?