Search code examples
androidshared-element-transition

Android shared element transition does not work


I have 2 Activities each having an ImageView.First one has an image which upon clicked should do the transition magic while moving to the next activity.It goes to the next activity,but for some strange reason transition is not working.

Activity1 Screenshot:

enter image description here

Activity2 Screenshot

enter image description here

Activity1.java:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
                {
                    Intent intent =new Intent(Activity1.this,Activity2.class);
                    ActivityOptionsCompat activityOptions=ActivityOptionsCompat.
                            makeSceneTransitionAnimation(Activity1.this,imageView,"shift");

                    ActivityCompat.startActivity(Activity1.this,intent,activityOptions.toBundle());
                }

activity1.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:layout_centerHorizontal="true"
    android:background="@drawable/th"
    android:transitionName="shift"/>
</RelativeLayout>

activity2.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/img1"
    android:layout_alignParentBottom="true"
    android:transitionName="shift"/>
</RelativeLayout>

Solution

  • You are not setting image for ImageView in your actvity2.xml

    Add this in your ImageView of actvity2.xml:

    android:background="@drawable/th"