+
+ // Set the support action bar.
+ setSupportActionBar(toolbar)
+
+ // Get a handle for the action bar.
+ val actionBar = supportActionBar!!
+
+ // Set a custom view on the action bar.
+ actionBar.setCustomView(R.layout.app_bar_textview)
+
+ // Display the custom view.
+ actionBar.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
+
+ // Define a hamburger icon at the start of the app bar. It will be populated in `onPostCreate()`.
+ actionBarDrawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer)
+
+ // Listen for touches on the navigation menu.
+ navigationView.setNavigationItemSelectedListener(this)
+
+ // Add a drawer listener.
+ drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
+ override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
+ // Do nothing.
+ }
+
+ override fun onDrawerOpened(drawerView: View) {
+ // Do nothing.
+ }
+
+ override fun onDrawerClosed(drawerView: View) {
+ // Reset the drawer icon when the drawer is closed. Otherwise, it is an arrow if the drawer is open when the app is restarted.
+ actionBarDrawerToggle.syncState()
+ }
+
+ override fun onDrawerStateChanged(newState: Int) {
+ // Do nothing.
+ }
+ })
+ }
+
+ override fun onPostCreate(savedInstanceState: Bundle?) {
+ // Run the default commands.
+ super.onPostCreate(savedInstanceState)
+
+ // Sync the state of the DrawerToggle after the default `onRestoreInstanceState()` has finished. This creates the navigation drawer icon.
+ actionBarDrawerToggle.syncState()