0

I'm trying to close naviagtion drawer on my project with this code:

   override fun onNavigationItemSelected(item: MenuItem): Boolean {
        drawerLayout.closeDrawer(GravityCompat.START)

        return true
    }

I have done done some research but it hasn't helped so far, I even tried this..

But for some reason it's not closing, please what might be wrong??

Edwin
  • 565
  • 11
  • 26

1 Answers1

1

I didn't add:

navigationView.setNavigationItemSelectedListener(this)

to the onCreate function.. I've added it, and it works fine..

Here's what it looks like;

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setSupportActionBar(toolbar)
    val toggle = ActionBarDrawerToggle(this ,drawerLayout, toolbar, R.string.open, R.string.close)
    toggle.isDrawerIndicatorEnabled = true
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    navigationView.setNavigationItemSelectedListener(this)
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    drawerLayout.closeDrawer(GravityCompat.START)

    return true
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Edwin
  • 565
  • 11
  • 26