Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 780
Farbod Salamat-Zadeh Avatar asked Jun 23 '26 05:06

Farbod Salamat-Zadeh


1 Answers

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" />
like image 194
Farbod Salamat-Zadeh Avatar answered Jun 25 '26 23:06

Farbod Salamat-Zadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!