how to remove bottom navigation view and toolbar in some fragments if using navigation controller?
More concise is to use a navigationlistener. This way you only need 1 function in your MainActivity and no code in all the fragments where you want to hide the bottomnavigation or any other UI element (like toolbar). Place this function inside your onCreate from your MainActivity.
My function looks like this:
private fun visibilityNavElements(navController: NavController) {
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.about_fragment,
R.id.settings_fragment,
R.id.detail_fragment,
R.id.missionPhotoFragment -> bottom_nav?.visibility = View.GONE
else -> bottom_nav?.visibility = View.VISIBLE
}
}
}
I use Kotlin Android Extensions to directly access views by there id (no findViewById needed).
how to hide BottomNavigationView on android-navigation lib
You could do something like this in your activity's onCreate. When ever an item in the nav bar is selected it will show or hide the nav based on the fragment id's.
private fun setupNav() {
val navController = findNavController(R.id.nav_host_fragment)
findViewById<BottomNavigationView>(R.id.bottomNav)
.setupWithNavController(navController)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.mainFragment -> showBottomNav()
R.id.mineFragment -> showBottomNav()
else -> hideBottomNav()
}
}
}
private fun showBottomNav() {
bottomNav.visibility = View.VISIBLE
}
private fun hideBottomNav() {
bottomNav.visibility = View.GONE
}
How to Hide Bottom Bar Nav.. When following single Activity design?
I added this line of code to the onCreate method:
// hiding bottom bar
navController.addOnDestinationChangedListener { _, nd: NavDestination, _ ->
// the IDs of fragments as defined in the `navigation_graph`
if (nd.id == R.id.navigation_home || nd.id == R.id.navigation_dashboard
|| nd.id == R.id.navigation_notifications
) {
navView.visibility = View.VISIBLE
} else {
navView.visibility = View.GONE
}
}
How to hide the bottom navigation bar in certain fragments?
For your fragment where it should be visible
navView.visibility = View.VISIBLE
Where it shouldn't be visible
navView.visibility = View.GONE
Hide Bottom Navigation View in fragment
If your code follows single activity design pattern then the following solution suites you.
- Create a method inside the parent activity to hide/show bottomNavigationView.
- Create a BaseFragment class(create your fragments by extending this BaseFragment Class)
- In the BaseFragment create a variable to hold the bottomNavigationViewVisibility (hide/show)
- In onActivityCreated method of the BaseFragment, get the activity reference and set the bottomNavigationViewVisibility by calling the method which we created in STEP1.
- In each fragment you create, just set the bottomNavigationViewVisibility variable.
Example:
In parentAcitivty layout, file add bottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"
app:menu="@menu/main_nav" />
Step 1: In parent activity, create a method to change the visibility.
fun setBottomNavigationVisibility(visibility: Int) {
// get the reference of the bottomNavigationView and set the visibility.
activityMainBinding.mainBottomNavigationView.visibility = visibility
}
Step 2 & 3 & 4:
abstract class BaseFragment : Fragment() {
protected open var bottomNavigationViewVisibility = View.VISIBLE
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// get the reference of the parent activity and call the setBottomNavigationVisibility method.
if (activity is MainActivity) {
var mainActivity = activity as MainActivity
mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
}
}
override fun onResume() {
super.onResume()
if (activity is MainActivity) {
mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
}
}
}
Step 5:
class SampleFragment1 : BaseFragment() {
// set the visibility here, it takes care of setting the bottomNavigationView.
override var navigationVisibility = View.VISIBLE
// override var navigationVisibility = View.GONE
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sampleFragment1, container, false)
}
}
How to hide bottom nav bar in fragment
To access your BottomNavigationView from within the fragments use the following code:
BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);
How to remove bottom navigation and tool bar using addOnDestinationChangedListener
Well after experimenting with a lot of stuff I got a solution... there wasn't much to be done a little changing in the code and it worked.
For toolbar:
navController.addOnDestinationChangedListener { _, destination, _ ->
when(destination.id) {
R.id.loginFragment, R.id.createAccountFragment ->
toolbar.visibility = View.GONE
else -> toolbar.visibility = View.VISIBLE
}
}
Similarly, for bottom navigation view we use :
navController.addOnDestinationChangedListener { _, destination, _ ->
when(destination.id) {
R.id.loginFragment, R.id.createAccountFragment ->
bottomNavigationView.visibility = View.GONE
else -> bottomNavigationView.visibility = View.VISIBLE
}
}
Related Topics
How to Convert Utc Timestamp to Device Local Time in Android
Force Video to Open in Youtube App on Android
Android Prevent Back Button from Closing the App
Disable Ssl as a Protocol in Httpsurlconnection
How to Disable Edittext in Android
How to Select Just One Radiobutton With Recyclerview Android
I Want to Change Actionbar Icon Size
Android: How to Turn Screen on and Off Programmatically
How to Check If Activity Is in Foreground or in Visible Background
Viewmodel Onchange Gets Called Multiple Times When Back from Fragment
How to Implement Referral Program in Mobile Apps for Both Android and Iphone
How to Turn Off Suggestions in Edittext
How to Load New Values of Json After Every 10 Seconds
How to Make a Background 20% Transparent on Android
Image Scale Type Center Crop on Flutter
Define Local_Src_Files in Ndk{} Dsl