So, the problem is that my hamburger button does not work when the application is started - it toggles, but the drawer menu doesn`t open. When I open and close the drawer menu by swiping from the side, it starts working... and so every time any new activity is started.
This is my kotlin code:
open class BaseActivity : AppCompatActivity(), Observer, NavigationView.OnNavigationItemSelectedListener {
private var drawer: DrawerLayout? = null
fun oncreate() {
val toolbar: android.support.v7.widget.Toolbar = this.findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val actionBar = this.supportActionBar
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setHomeButtonEnabled(true)
}
//val actionbar: ActionBar? = supportActionBar
drawer = findViewById(R.id.drawer_layout)
val navigationView: NavigationView = findViewById(R.id.nav_view)
navigationView.setNavigationItemSelectedListener(this)
toggle = ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
actionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setHomeAsUpIndicator(R.drawable.ic_menu)
}
drawer!!.addDrawerListener(toggle)
toggle.syncState()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
drawer!!.openDrawer(GravityCompat.START)
true
}
else -> super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
drawer!!.openDrawer(GravityCompat.START)
true
}
//other items
}
drawer!!.closeDrawer(GravityCompat.START)
return true
}
And the xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/background"
tools:openDrawer="start" android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="visible"/>
//more elements here
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:itemTextAppearance="@style/txt_expandable"
app:menu="@menu/drawer_menu" android:visibility="gone">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
I`ve tried a lot of things to solve the problem, but nothing seems to work... I followed exactly the steps in the Kotlin documentation and everything, but the problem stays.
Here is error:
app:menu="@menu/drawer_menu" android:visibility="gone">
Remove android:visibility="gone"
. Should be:
app:menu="@menu/drawer_menu">