0

I tried to follow some tutorials on how to set up a shared elements transition. Though when i try to set up a background the shared element kind of glitches the wrong way and then moves like it should.

Heres a link to a video of my problem: https://drive.google.com/file/d/14lTFGIpqnMwM2MEbLxwBe_gkI0GuyuMc/view?usp=sharing

When no background is set: https://drive.google.com/open?id=15Hpkj7y8nLLTXa5RF5FV00jlTTWMofjF

When i don't set a background in my xml layout, the animation works properly. Does anyone know, why this happens?

Activity 1 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/colorPrimary"
    android:orientation="vertical"
    tools:context=".MainActivity">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nexa"
            android:gravity="center"
            android:padding="10dp"
            android:text="Test"
            android:textColor="@color/colorAccent"
            android:textSize="30sp"
            android:transitionName="transition"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity 2 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="Test"
            android:fontFamily="@font/nexa"
            android:textColor="@color/colorAccent"
            android:textSize="30sp"
            android:transitionName="transition"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.05" />


</androidx.constraintlayout.widget.ConstraintLayout>

Activity 1 Java

package com.stonks.sharedanimtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        getWindow().setEnterTransition(null);

        textView = (TextView) findViewById(R.id.text1);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(MainActivity.this, MainActivity2.class);

                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, textView, "transition");

                startActivity(intent, options.toBundle());

            }
        });


    }
}

Activity 2 Java

package com.stonks.sharedanimtest;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    private TextView textView;

    @Override
    public void onBackPressed() {
        textView = (TextView) findViewById(R.id.text2);

                Intent intent = new Intent(MainActivity2.this, MainActivity.class);

                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity2.this, textView, "transition");

                startActivity(intent, options.toBundle());

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        getWindow().setEnterTransition(null);
    }
}

Colors XML

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#161618</color>
    <color name="colorPrimaryLight">#202024</color>
    <color name="colorAccent">#ffffff</color>
</resources>

Manifest XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stonks.sharedanimtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity2"
            android:screenOrientation="portrait" />
        <activity android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Khamp3r
  • 25
  • 5

1 Answers1

1

You can wrap your shared view (either text1 or text2) in FrameLayout - but still TextView should be the shared element. It should solve the issue.

mdabrowski89
  • 21
  • 1
  • 4
  • Please add a bit of explanation on why/how this solves OP's problem. – vonbrand Mar 30 '20 at 22:07
  • Well, at the moment I have no idea, I've just had similar issue and I've noticed that wrapping it up solvs the issue. I'm still investigating the interanls of Android API to find a reason - I will post it if I found it. Here is a related question [link](https://stackoverflow.com/questions/26899779/enter-transition-on-a-fragment-with-a-shared-element-targets-the-shared-element) but I was not able to fix it with described solution. – mdabrowski89 Mar 31 '20 at 08:36
  • I updated constaintlayout to 2.0.4 and it broke a transition in my app. This fixed it. No idea why it broke or why this fixed it, but thanks! – Dan Apr 12 '21 at 23:01