Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top Toolbar Menu + BottomAppBar Menu + BottomNavigationDrawerFragment Just like Material Design

What I want to achieve is like this image (Without top NavigationView though) but with Toolbar Menu + BottomAppBar Menu + BottomNavigationDrawerFragment exactly like material design:

enter image description here

I could manage the BottomAppBar menu by replace() (My Answer):

val bottomBar = findViewById<BottomAppBar>(R.id.bottomAppBar)
bottomBar.replaceMenu(R.menu.menu_main)

Which allow me to inflate menu for the BottomAppBar and used below codes plus onCreateOptionsMenu() for the Toolbar Menu and setSupportActionBar():

val toolbar = findViewById<Toolbar>(R.id.myToolbar)
setSupportActionBar(toolbar)

The point is, in this tutorial (for example), he used setSupportActionBar(bottom_app_bar) for setting SupportActionBar on the BottomAppBar. So, if we use setSupportActionBar(bottom_app_bar) for the BottomAppBar, it will show the BottomNavigationDrawerFragment + Menus are handlable on the Bottom Side.

But, what about Toolbar and menus? Toolbar + menu items won't be handlable nor showing up if we use setSupportActionBar(bottomAppbar).

The things that I have tested are:

  • Might sound ridiculous but used two setSupportActionBar() for both Toolbar and BottomAppBar
  • Even tried to inflate two menus by onCreateOptionsMenu() method but none worked.

The question is, How can we have Top Toolbar Menu + BottomAppBar Menu + BottomNavigationDrawerFragment all together?

Any thoughts?

like image 821
ʍѳђઽ૯ท Avatar asked Sep 25 '18 22:09

ʍѳђઽ૯ท


People also ask

What's new in bottomappbar?

Bringing navigation drawer control and action menu to the bottom of an app, BottomAppBar suggests a fresh new design to Android apps. Together with BottomAppBar, Floating Action Button (FAB) placement has also been changing. With the new design, FABs can be placed as either being cradled/docked into some notch of the bar or overlapping the bar.

Can the bottom app bar be used as a navigation bar?

A bottom app bar can display a navigation menu icon to open a bottom navigation drawer, but the bar doesn't contain any navigation actions itself (such as Up navigation to a home screen or a close icon). App navigation should be placed in another component such as a top app bar, or embedded on-screen.

What does the bottom navigation drawer open from?

This bottom navigation drawer opens from a bottom app bar. The drawer opens in front of the bottom app bar, and displays a top app bar to close the drawer when it reaches full height. The bottom app bar can be covered by keyboards and other temporary surfaces.

What are bottom app bar component APIs?

Android's bottom app bar component APIs provide support for the navigation icon, action items, overflow menu and more for informing the user as to what each action performs. While optional, their use is strongly encouraged.


1 Answers

Hopefully, I have found the answer. So if we want to create such layouts (Without Top NavigationDrawer - view) Here are the steps:

Declare the Toolbar as usual:

val toolbar = findViewById<Toolbar>(R.id.myToolbar)
setSupportActionBar(toolbar)

Override onCreateOptionsMenu with the Top Toolbar menu:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.top_menu, menu)
    return super.onCreateOptionsMenu(menu)
}

Use the following for handling BottomBar menus and replacing a new Menu in Bottom of the app & showing BottomSheetFragment when NavigationIcon selected:

val bottomBar = findViewById<BottomAppBar>(R.id.bottomAppBar)
bottomBar.replaceMenu(R.menu.bottom_menu)
bottomBar.setNavigationOnClickListener {
        val bottomNavDrawerFragment = BottomNavigationDrawerFragment()
        bottomNavDrawerFragment.show(supportFragmentManager, bottomNavDrawerFragment.tag)
    }
bottomBar.setOnMenuItemClickListener { menuItem ->

        when (menuItem.itemId) {
            R.id.search_Action ->{
                Toast.makeText(this@MainActivity, "Clicked", Toast.LENGTH_LONG).show()
            }
        }
         true
    }

And finally, Overriding onOptionsItemSelected():

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    when (item!!.itemId) {

        R.id.action_settings -> {

            startActivity(Intent(this@MainActivity, SettingsActivity::class.java))

        }

        R.id.changeView -> {
            toast("Test")

        }
    }
    return true
}
like image 80
ʍѳђઽ૯ท Avatar answered Sep 30 '22 15:09

ʍѳђઽ૯ท