Menu & Toolbar

ahasanpapon

Papon Ahasan

Posted on February 10, 2023

Menu & Toolbar

Android Toolbar Adding Menu Items for different fragments

Activity

        // Add menu items without overriding methods in the Activity
        addMenuProvider(object : MenuProvider {
            override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
                // Add menu items here
                menuInflater.inflate(R.menu.options_menu, menu)
            }

            override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
                // Handle the menu selection
                return true
            }
        })
Enter fullscreen mode Exit fullscreen mode

Fragment

        val menuHost: MenuHost = requireActivity()

        menuHost.addMenuProvider(object : MenuProvider {
            override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
                // Add menu items here
                menuInflater.inflate(R.menu.recyclerviw_menu, menu)
            }

            override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
                // Handle the menu selection
                return true
            }
        }, viewLifecycleOwner, Lifecycle.State.RESUMED)
Enter fullscreen mode Exit fullscreen mode

hide-toolbar-in-specific-fragment

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
ahasanpapon
Papon Ahasan

Posted on February 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About