combine navigation up button with drawer navigation hamburger button inside toolbar - android

I am struggling in combining both up button to navigate back through my fragments and the overflow menu "hamburger" button that can open up the navigation drawer menu..
any suggestion how to make it work and have the menu icon on the right and the up button on the left like in the following picture
?
MainActivity.onCreate part:
setSupportActionBar(binding.toolbar)
.apply {
title = null
}
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
my Toolbar xml :
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
style="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/toolbarColor"
app:navigationIcon="#drawable/ic_baseline_menu_24" />
I tried adding
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration.Builder(R.id.foundLostFragment)
NavigationUI.setupActionBarWithNavController(
this,
navController,
appBarConfiguration.build()
)
but it just makes the up button act like the hamburger button so it opens the drawer menu and I cannot manage to have both of the icons displayed on the toolbar..

What I have done to fix my issue was creating a second menu for the toolbar itself with 1 menu item and overrided onOptionsItemSelected() to apply that when the toolbar item clicked I just open the drawer menu so in that way I have both up button and both menu item that opens my drawer layout menu :)

Related

Android - Custom handling for Navigation Up while using CollapsingToolbar with Navigation Component

Hello I am trying to integrate the collapsing toolbar with navigation component and add custom handling for back/navigation up in some screens.
navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.findNavController()
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
navGraph = navController.graph,
fallbackOnNavigateUpListener = {
false
}
)
appBarLayout = findViewById(R.id.appBarLayout)
val layout = findViewById<CollapsingToolbarLayout>(R.id.collapsing_toolbar_layout)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
layout.setupWithNavController(toolbar, navController, appBarConfiguration)
I used this to initialize my layout, and I noticed that I need to use layout.setupWithNavController to allow the collapsing toolbar to read the title from NavGraph and without that my title is not being updated at all!
I had a onBackPressedDispatcher listener that was working fine for hardware Back button and Home/NavUp button with setupActionBarWithNavController(navController, appBarConfiguration), however after using CollapsingToolbar and switching to collapLayout.setupWithNavController(toolbar, navController, appBarConfiguration) I found that my backDispatcher is not working, and therefore my custom back handler is not working either for NavUp button.
I tried to see if functions onSupportNavigateUp onOptionsItemSelected onBackPressed is being triggered, but none of them are anymore if I click on NavUp.
What is the proper way to add custom handler for NavUp while keeping the Collapsing toolbar with the NavComponent?
Is this even possible?

Android jetpack navigation custom back button overlapping back arrow on back

Using android jetpack navigation in combination with toolbar and drawer is that the root destination has a hamburger menu icon (to toggle the drawer) and in child fragments there is a back button.
Also an animation exists when opening / closing child fragments on the back arrow.
Now the problem: In one of my child fragments I set a custom navigation back button
toolbar_main.setNavigationIcon(R.drawable.ic_clear)
This also works, but upon closing there is a "glitch" where
The custom icon disappears
The back arrow is visible for a short while (this is the "glitch"
The child closes and the root fragment (with burger icon) is visible again
Question:
Is this "glitch" a bug or do I have to call something other than setNavigationIcon (like ActionBarDrawerToggle or similar) ?
Solution: in each Fragment
override fun onAttach(context: Context) {
super.onAttach(context)
val activity = context as BaseActivity
if (navController.backStack.size > 3) {
activity.toolbar.setNavigationIcon(getNavigationIcon())
}
}
more than 3 because:
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation"
app:startDestination="#+id/startFragment"> <----- always +1
<fragment
android:id="#+id/startFragment"> <----- startFragment + 1 = 2
<fragment
android:id="#+id/fragment_1"> <----- startFragment + 1 = 3
<fragment
android:id="#+id/fragment_2"> <----- startFragment + 1 = 3
</navigation>

android add expand/collapse in navigation drawer menu

Hey guys this is my navigation drawer menu items.
NavigationView nav = (NavigationView) findViewById(R.id.nav_view);
final DrawerLayout drawerlayout = (DrawerLayout)
findViewById(R.id.drawer_layout);
Menu menu = navigationView.getMenu();
SubMenu sub = menu.addSubMenu("Menu 1");
sub.add("Menu 1.1");
sub.add("Menu 1.2");
sub.add("Menu 1.3");
drawerlaayout.closeDrawers();
This how i create menu items.now i Want to add Expand/collapse option in each menu items.Menu 1,Menu 2,Menu 3.
you could try these by doing r&d
1.try whether we can insert our custom layout for menu. if so, change the visibility of submenu.
2.direct drawable insert of icon into the menu.
if nothing helps try,
1.try with expandable recyclerview big nerd's library. obviously this will work.
2.click in the menu header.

setDrawerLockMode not working in android?

I want to lock swipe left-right and right-left of DrawerLayout.
DrawerLayout drawerLayout;
onCreate:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Click button to open drawerLayout:
drawerLayout.openDrawer(Gravity.LEFT);
and close:
drawerLayout.closeDrawers();
I try setDrawerLockMode in onCreate, onResume, onStart but not working, it still can open, close by swipe it.
Edit 2:
It work with:
android:layout_gravity="start"
and not work with:
android:layout_gravity="start|bottom"
Any helps. Thanks.
Add gravity value too when using setDrawerLockMode();
Do this :
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
This should work

Menu icons toolbar on Android 5.0 (Lollipop)

I can't load the menu items in the toolbar. I'm using the Drawer Navigation, and I can't even show the hamburger icon.
I'm using getSuportActionBar, my activity extends from ActionBarActivity, I added the toolbar xml into my activity xml.
SOLUTION
I found my solution here, I just added a LinearLayout as a parent in the toolbar
Appcompat Toolbar Not Showing With Navigation Drawer
You can set your favorite icon and add a listener.
mToolbar.setNavigationIcon(R.drawable.ic_drawer);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDrawerLayout.openDrawer(Gravity.START);
}
});
I found my solution here, I just added a LinearLayout as a parent in the toolbar
Appcompat Toolbar Not Showing With Navigation Drawer

Categories

Resources