Android shared element transition between two activities does not work
Asked Answered
M

3

21

In my app I'm trying to use the newly introduced element sharing between activities. Everything works like a charm if the shared element is with fixed position (e.g. android:layout_gravity="top") but the problem comes when the view is anchored.

My first activity looks like this:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:auto="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        ...
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/play_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="24dp"/>

</android.support.design.widget.CoordinatorLayout>

My second activity looks like this

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:auto="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:elevation="10dp"
        android:src="@drawable/ic_action_play"
        auto:layout_anchor="@+id/appbar"
        android:transitionName="fab_button"
        auto:layout_anchorGravity="bottom|right" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="192dp">
        ...
    </android.support.design.widget.AppBarLayout>

    ...

</android.support.design.widget.CoordinatorLayout>

The code I use is as follows:

Intent intent = ...;
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, view, "fab_button");
startActivity(intent, options.toBundle());

If I use the layout_anchor and layout_anchorGravity attributes the transition between the two FABs is done with no animation. If the second FAB is with fixed position, it works perfectly. What am I doing wrong?

Mages answered 4/8, 2015 at 8:20 Comment(4)
I think you are not doing anything wrong. My app is very very similar to yours for these two activities, and I have the same problem. I think that shared element transition framework can't get the position of the anchored element… I hope someone can find a solution thoughJinn
I see that there are applications where this behavior works well. I guess they are doing some custom logic for achieving this effect.Mages
Thank you for your report, been scratching my head for hours against this. As I've removed the app:layout_anchor the transition was smooth as it needed to be. How is it possible this matter was so hard to discover and hasn't been reported yet... Come on..Nicolais
You should report this bug in android's bug tracker under Support LibsJinn
H
16

This might be a bit late, but I found a way around the issue. You have to wrap your shared element into a layout, and put the anchor on that layout:

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        auto:layout_anchor="@+id/appbar"
        auto:layout_anchorGravity="bottom|right">

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:elevation="10dp"
            android:src="@drawable/ic_action_play"
            android:transitionName="fab_button" />

<FrameLayout/>
Hobbs answered 23/2, 2016 at 5:23 Comment(1)
Is it a bug in coordinatorLayout + shared transitions? But it works like a charm :)Microfarad
S
1

Activity Shared Elements Transitions:

Follow the steps.

  1. Enable Window Content Transitions
  2. Assign a Common Transition Name
  3. Start Activity
  4. Multiple Shared Elements
  5. Customizing Shared Elements Transition

I think you do not have follow the second step. You had given android:transitionName="fab_button" in Second Activity but not given in First Activity.

XML of First Activity (added android:transitionName="fab_button"):

<android.support.design.widget.FloatingActionButton

        android:transitionName="fab_button"

        android:id="@+id/play_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="24dp"/>

May above links will helps you.

Semantics answered 21/10, 2015 at 12:7 Comment(1)
Nope, that's not it. The first activity view is an imageview and the destination is an anchored FAB. If I make the destination FAB non-anchored everything works fine.Urbannai
P
0

You should have transition name in receiving activity also. Check my code.

First Activity

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_register"
        android:transitionName="@string/transition"/>

</android.support.design.widget.CoordinatorLayout>

And Second Activity xml

<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:transitionName="@string/transition"
tools:context="com.example.shoppingappdemo.RegiserActivity">
Ping answered 21/10, 2015 at 12:30 Comment(3)
Same what i have given in my answer.Semantics
Sorry for that..actually my net was slow so after i have posted my answer i show urs...sorry for thatPing
That's not the issue. Please read the question, everything works fine if the destination FAB is un-anchored. The problem seems to be when the destination FAB is anchored.Urbannai

© 2022 - 2024 — McMap. All rights reserved.