Animating the action bar in fragment transitions

Viewed 16

I have an app which is using the one-activity-multiple-fragments architecture using navigation component.

For each fragment, I have a slide in/out animation like so:

anim_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%p"
    android:toXDelta="0%"
    android:duration="@android:integer/config_shortAnimTime" />

anim_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%p"
    android:toXDelta="100%"
    android:duration="@android:integer/config_shortAnimTime" />

nav_graph.xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.monettests.MainFragment"
        android:label="@string/app_name"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/action_mainFragment_to_settingsFragment"
            app:enterAnim="@anim/anim_in"
            app:popEnterAnim="@anim/anim_in"
            app:popExitAnim="@anim/anim_out"
            app:destination="@id/settingsFragment" />
        <action
            android:id="@+id/action_mainFragment_to_feedbackFragment"
            app:enterAnim="@anim/anim_in"
            app:popEnterAnim="@anim/anim_in"
            app:popExitAnim="@anim/anim_out"
            app:destination="@id/feedbackFragment" />
    </fragment>
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.monettests.SettingsFragment"
        android:label="Settings"
        tools:layout="@layout/fragment_settings" />
    <fragment
        android:id="@+id/feedbackFragment"
        android:name="com.example.monettests.FeedbackFragment"
        android:label="Send Feedback"
        tools:layout="@layout/fragment_feedback" />
</navigation>

Navigation code in MainFragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    requireActivity().addMenuProvider(object : MenuProvider {
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
            menuInflater.inflate(R.menu.menu_main, menu)
        }

        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            return when (menuItem.itemId) {
                R.id.menuMain_settingsItem -> {
                    findNavController().navigate(R.id.action_mainFragment_to_settingsFragment)
                    true
                }

                R.id.menuMain_feedbackItem -> {
                    findNavController().navigate(R.id.action_mainFragment_to_feedbackFragment)
                    true
                }

                else -> {
                    false
                }
            }
        }
    }, viewLifecycleOwner, Lifecycle.State.RESUMED)
}

The problem with this is that it just animates the fragment itself but not the action bar.

What I have tried:

  • Manually animating the action bar in each fragment using listeners (unsuccessful)
  • Searching documentation for a way to do this (tried for hours but unsuccessful - shocked that there isn't a way to do this in the SDK)

I understand that this is desired fragment behavior, but I want to give the end user the illusion of animating between different pages and for that it's best if the action bar itself is part of the transition.

Is there any way to have a fragment transition that animates the fragment and the action bar, similar to how it's done with activities?

Any help would be appreciated.

0 Answers
Related