What I want to achieve is like this image (Without top NavigationView
though) but with Toolbar
Menu + BottomAppBar
Menu + BottomNavigationDrawerFragment
exactly like material design:
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:
setSupportActionBar()
for both Toolbar
and BottomAppBar
onCreateOptionsMenu()
method but none worked. The question is, How can we have Top Toolbar
Menu + BottomAppBar
Menu + BottomNavigationDrawerFragment
all together?
Any thoughts?
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.
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With