Search code examples
androidkotlinmenuitemdrawer

How can I remove a MenuItem if user is logged in or change the title?


I have a nav drawer in my app and I have a few items, and I want the "Register" item to be invisible if the user logs in and a "login" title to change to "logout. I searched for ways to do this but I kept getting errors.

It looks like this: drawer

I have a class where I store my nav code inside that I call in other activities that looks like this:

class NavigationHandler(val context: Context, val item: MenuItem) : AppCompatActivity() {
    operator fun invoke() {
        when (item.itemId) {
            R.id.nav_latestAds -> {
                val homeIntent = Intent(context, MainActivity::class.java)
                context.startActivity(homeIntent)
            }
            R.id.nav_cars -> {
                val carListIntent = Intent(context, CarsList::class.java)
                context.startActivity(carListIntent)
            }
            R.id.nav_properties -> {
                val carListIntent = Intent(context, PropertyList::class.java)
                context.startActivity(carListIntent)
            }
            R.id.nav_mobiles -> {
                val intent = Intent(context, MobileList::class.java)
                context.startActivity(intent)
            }
            R.id.nav_electDev -> {
                val homeIntent = Intent(context, ElectricsList::class.java)
                context.startActivity(homeIntent)
            }
            R.id.nav_furniture -> {
                val intent = Intent(context, FurnitureList::class.java)
                context.startActivity(intent)
            }
            R.id.nav_vendorReg -> {
                val intent = Intent(context, RegisterVendor::class.java)
                context.startActivity(intent)
            }
            R.id.nav_settings -> {
                val intent = Intent(context, Settings::class.java)
                context.startActivity(intent)
            }
            R.id.nav_logout -> {
                if (AuthService.isLoggedIn) {
                    UserDataService.logout()
                } else {
                    val loginIntent = Intent(context, Login::class.java)
                    context.startActivity(loginIntent)
                }
            }
        }
    }
}

Here is how I call it in other activities:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        NavigationHandler(this, item)()
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

is it wrong by me to build the drawer like this?


Solution

  • You will have to keep references to the menu items you want to mutate(change name, make invisible etc). To achieve this use follwoing code in your activity

     // Declate two variables in your activity
     private lateinit var register: MenuItem
     private lateinit var logIn: MenuItem
    
    // Now initialize them in your activity's onCreate as follows
    var navigationView = findViewById(R.id.idOfNavigationView)
    register = navigationView.menu.findItem(R.id.idOfRegister)
    logIn = navigationView.menu.findItem(R.id.idOfLogin)
    

    Now you have your menu items and you can change their title or make them invisible. For example if you want to hide the register menu item and change the title of login, you will do the following

    // Once user has logged in successfully, use following lines to change menu items as you wish
    register.isVisible = true
    logIn.title = "logout"