Programmatically expand/collapse Bottom Navigation View in CoordinatorLayout [Kotlin]

Viewed 183

Prior to this question, setVisibility works but the problem is,

visibility = true, after hiding(using visibility = gone) the BottomNavigationView, shows up a blank space

the blank space is the space of the BNV, but no layout is inflated in it, due to the HideViewOnScrollBehavior

Q: How to expand the BNV programmatically

2 Answers

Here with Transition effect, first place this fade_in_out.xml file in your transition resource folder.

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:transitionOrdering="sequential">
    <fade android:fadingMode="fade_out" />
    <changeBounds />
    <fade android:fadingMode="fade_in" />
</transitionSet>

Then create two global variable in your Activity or Fragment as follows:

private var expanded = false
private lateinit var toggle: Transition

Then initialize the toggle variable in your onCreate() like:

toggle = TransitionInflater.from(requireContext()).inflateTransition(R.transition.fade_in_out)

Then use this toggleExpanded() function to expand or collapse your collapsible view.

fun toggleExpanded() {
    expanded = !expanded
    toggle.duration = if (expanded) 200L else 150L
    // Here rootView is the root layout of your layout resource file
    TransitionManager.beginDelayedTransition(binding.rootView as ViewGroup, toggle)
    // 'expandableView' is the view that expands and collapse
    binding.expandableView.visibility = if (expanded) View.VISIBLE else View.GONE
}
Related