Search code examples
androidandroid-jetpackandroid-architecture-navigation

How to add animation transition to NavigationUI in android?


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.


Solution

  • 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);