1

i'm using NavigationUI to tie destinations to menu items, but how to override the default animation transition?

Based on the doc https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#Tie-navdrawer, i cant find any method that can add the animation transition.

Alexsa
  • 23
  • 4

3 Answers3

5

NavigationUI does not offer that API. However, there's absolutely no requirement to use NavigationUI - it is only optional helper methods.

Therefore you can copy / build a simplified version of what it actually does:

NavOptions navOptions = new NavOptions.Builder()
    .setLaunchSingleTop(true)  // Used to prevent multiple copies of the same destination
    .setEnterAnim(R.anim.your_enter_anim)
    .setExitAnim(R.anim.your_exit_anim)
    .setPopEnterAnim(R.anim.your_pop_enter_anim)
    .setPopExitAnim(R.anim.your_pop_exit_anim);
    .build();

// Assuming you have a MenuItem named item
navController.navigate(item.getItemId(), null, options);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
0

This is an old thread but recently i encountered same problem . Here is the ideal solution to override default transition animations in NavigationUI

Click Here

Shrey Gupta
  • 392
  • 4
  • 10
0

I have solved the issue, by creating default navigation animation resources in the 'animator' resource directory. I have created these four animator resource files-

  • res/animator/nav_default_enter_anim.xml
  • res/animator/nav_default_exit_anim.xml
  • res/animator/nav_default_pop_enter_anim.xml
  • res/animator/nav_default_pop_exit_anim.xml

The rests are the same, just follow the Google's official guide to set up bottom navigation bar.

bottomNavBar = binding.bottomNavBar
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fNavHost) as NavHostFragment

        NavigationUI.setupWithNavController(bottomNavBar, navHostFragment.navController)
Naimul Kabir
  • 434
  • 6
  • 13