Farbod Salamat-Zadeh
Farbod Salamat-Zadeh

Reputation: 20140

Android shared element transitions not working with <include> tag

I've been using shared element transitions in my app using ActivityOptionsCompat.makeSceneTransitionAnimation() and ActivityCompat.startActivityForResult() with some of the following XML code:

...

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:transitionName="@string/transition_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16sp"
    android:paddingBottom="16sp">

    ...

</android.support.v7.widget.CardView>

...

Everything here works great.

However, since I am using the content inside my CardView multiple times, I decided to move it to a new layout and use <include> to reference it.

Here's the new layout file:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    ...

</android.support.v7.widget.CardView>

And the old file:

...

<include layout="@layout/my_card_content"
    android:transitionName="@string/transition_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16sp"
    android:paddingBottom="16sp" />

...

Now, for some reason, the shared element transitions don't seem to work.

How should I fix this issue?

Upvotes: 3

Views: 695

Answers (1)

Farbod Salamat-Zadeh
Farbod Salamat-Zadeh

Reputation: 20140

Turns out the problem is because I need to include the android:transitionName inside the layout with my card so that my card layout looks like this:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:transitionName="@string/transition_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    ...

</android.support.v7.widget.CardView>

Meaning that it is not needed in my <include> tag:

<include layout="@layout/my_card_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16sp"
    android:paddingBottom="16sp" />

Upvotes: 4

Related Questions