Programmatically expand/collapse Bottom Navigation View in CoordinatorLayout - android

I have a CoordinatorLayout that contains a BottomNavigationView and an AppBarLayout with a ToolBar inside of it. (The BottomNavigationView is not inside the AppBarLayout, as doing it breaks the position of the BottomNavigationView).
I need to show/hide the AppBarLayout and the BottomNavigationView programmatically, when certain events happen (e.g. on a fragment's onResume), and so far I've managed setting appBarLayout.setExpanded(true, true) in order to show/hide the AppBar, but I can't figure out how to do the same for the BottomNavigationView, as it does not have any method to show/hide itself.
The behaviour of my BottomNavigationView is app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior", set in the layout xml. How can I get a reference to this behaviour in my code in order to manage its expanded/collapsed state?

Thats code slides up your bottom navigation that are inside of a Coordinator Layout and have a HideBottomViewOnScrollBehavior.
If you are using findViewById just replace that for every binding.
val layoutParams = binding.bottomNavigation.layoutParams as CoordinatorLayout.LayoutParams
val bottomViewNavigationBehavior = layoutParams.behavior as HideBottomViewOnScrollBehavior
bottomViewNavigationBehavior.slideUp(binding.bottomNavigation)

You can hide bottomNavigation view from your fragment container activity. Because of you have bottomNavigation id. and you can access this id from your fragment container activity and also you can get the current fragment instance from your fragment container activity. So from your example if you want to hid bottom navigation from particular fragment onResume() event you can check that which fragment is currently visible from you Fragment container activity and call hide or show bottom navigation.
Remember for hiding or showing bottom navigation use animation.

Related

Hiding a BottomNavigationView during a fragment transition

I am creating an app using single Activity so on main_activity I set the fragment as NavHostFragment and on the bottom the BottomNavigationView.
In fragment A (first tab selected), If I click on button_1...navigate on a new fragment (A1). Of course inside fragment A1 I need to hide BottomNavigationView so on main_activity I am using addOnDestinationChangedListener to hide BottomNavigationView.
The problem is that if I want use a transition (enter, exit, popup_enter, popup_exit) the BottomNavigationView hide immediatly. (example 1 on image)
How can I do to have the same as example 2 on image?
It looks like you have your 'NavHostFragment' below the 'BottomNavigationView'. So even if you were to delay hiding the BottomNavigationView the fragment transition would still happen behind it. So without changing your implementation completely it won't be possible to have the new fragment transition above the bottom BottomNavigationView.

Jetpack BottomNavigationView How to remove content fade in animation

I'm creating a single activity applicatition. Inside Activity there is a BottomNavigationView
with NavHostFragment. Bottom navigation view comprise of multiple menu items.
For easy to explain lets say there are three fragments "FragmentOne", "FragmentTwo", "FragmentThree"
each fragment have a different Toolbar. As a example "FragmentOne" is haveing a Toolbar with CollapingToolbarLayout, and "FragmentTwo" has a Normal Toolbar Design and "FragmentThree" has a toolbar with Image.
I have included different design Toolbars in each fragment and the BottomNavigationView is inside Activity.
Where select each bottom navigation menu, fragments are navigating correctly but content is showing fade in animation/transition.
I have set the app theme as Theme.AppCompat.Light.NoActionBar So now since Toolbar is also inside the fragment content area Toolbar also showing fade in transition.I'm okay with content transition but Toolbar should stay Solid.
Kindly help to me to solve this and feel free to request more information.

Hide snackBar from one fragment, when change tab in viewpager

I have a viewpager with 2 fragments. In the second fragment, I am displaying snackbar (using a view in the fragment). I would like to hide that snackbar when the user swipes to the first tab. I don't know why if the bar is being displayed in a layout of a fragment, I can see it in other fragment.
For viewpager when user swipes from one screen to another screen(fragment)
ViewPager.OnPageChangeListener() will gets invoked you can handle here.
Use CoordinatorLayout for your fragment.
As per android documentation:
"The method actually searches up the view hierarchy from the passed view until it reaches either a CoordinatorLayout, or the window decor's content view"
Using coordinatorLayout for your fragment will attach the snackbar to the fragment, so when you swipe it wont overlap with other fragment.
If your fragment is using other than CoordinatorLayout, enclose your layout with CoordinatorLayout.
Documentation link https://developer.android.com/training/snackbar/showing

Swap toolbar on fragment launch

I have an activity with a NavigationDrawer and a full screen fragment. Clicking on various items in the NavigationDrawer inflates a different fragment in the activity.
I would like to also swap in a different toolbar when launching a different fragment. The reason I want to do that instead of inflating a new menu is that the toolbar I want to swap in is a bit complicated and has things like an EditText in it.
Is there some way to do this either in the activity before inflating a fragment or maybe in the fragment?
Just include your Fragment specific toolbar in your Fragment's layout so that it's visible in your Fragment permanently.
Now in the onStart() method of your Fragment -
getActivity().getSupportActionBar().hide();
This will hide the activity's default toolbar so that it does not overlap with the Fragment's toolbar.
Then again in the onStop() method of your Fragment -
getActivity().getSupportActionBar().show();
So that the Activity's toolbar is visible again outside that Fragment.
each time you call a fragment use "invalidateOptionsMenu" and using fragment id you can set visibility of menuitems in toolbar... if that is what u need... hope it helps

How to implement collapsing Toolbar with fragments and NavigationDrawer?

I have an application with Activity, Fragment inside activity, Toolbar and Navigation Drawer.
I need a Toolbar will hiding when I scroll content of fragment.
Following manual I need to put ToolBar and scrollable content inside same XML file. But my scrollable content placed in Fragment and ToolBar placed in Activity. How to implement collapsing Toolbar?
You need to use CoordinatorLayout
Try to use this guide. There's good explanation

Categories

Resources