Sambhav Khandelwal
Sambhav Khandelwal

Reputation: 3765

Why is my activity transition not working?

I want to animate a EditText from 1 activity to another. This is my edit text on the first activity:

<EditText
        android:id="@+id/urlEditText"
        android:layout_width="0dp"
        android:layout_height="40dp"
        app:layout_constraintTop_toBottomOf="@id/topLayout"
        android:background="@drawable/gray_rounded"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginHorizontal="25dp"
        android:hint="Search or type a type a url"
        android:paddingEnd="20dp"
        android:drawableStart="@drawable/search"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:layout_marginTop="25dp"
        android:transitionName="shift"/>

And this is in my second activity:

<EditText
        android:id="@+id/urlEditText"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@drawable/gray_rounded"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginHorizontal="25dp"
        android:hint="Search or type a type a url"
        android:paddingEnd="20dp"
        android:drawableStart="@drawable/search"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:layout_marginTop="25dp"
        android:transitionName="shift"/>

But, in the fist one, it is placed at the bottom and at the second activity, at the top. This is what I tried to start the animation:

Intent intent = new Intent(MainActivity.this,FullSearchActivity.class);
ActivityOptionsCompat activityOptions= ActivityOptionsCompat.
   makeSceneTransitionAnimation(this,binding.urlEditText,"shift");

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

But this does not open the second activity. To verify that it opens or not, I tried to show toast in the second activity. The toast is displayed but the second activity is not shown. Why does this happen?

Edit

This is my full ResultActivity code:

public class ResultActivity extends AppCompatActivity {
    
    ActivityResultBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityResultBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }
}

PS: The FullSearchActivity has been renamed by me to ResultActivity

Upvotes: 1

Views: 1256

Answers (2)

Ashvin solanki
Ashvin solanki

Reputation: 4809

Specify custom transitions

First, enable window content transitions with the android:windowActivityTransitions attribute when you define a style that inherits from the material theme. You can also specify enter, exit, and shared element transitions in your style definition:

<style name="BaseAppTheme" parent="android:Theme.Material">
  <!-- enable window content transitions -->
  <item name="android:windowActivityTransitions">true</item>

  <!-- specify enter and exit transitions -->
  <item name="android:windowEnterTransition">@transition/explode</item>
  <item name="android:windowExitTransition">@transition/explode</item>

  <!-- specify shared element transitions -->
  <item name="android:windowSharedElementEnterTransition">
    @transition/change_image_transform</item>
  <item name="android:windowSharedElementExitTransition">
    @transition/change_image_transform</item>
</style>

For More : https://developer.android.com/training/transitions/start-activity

Upvotes: 2

Syed Faizan Ali
Syed Faizan Ali

Reputation: 389

Here is the working code in Kotlin. You can convert it into Java.

val animationIntent = Intent("FIRST ACTIVITY", SECOND ACTIVITY::class.java)
    mViewBinding.apply {
        val options = ViewCompat.getTransitionName("EDIT TEXT ID HERE OF FIRST ACTIVITY")?.let {
            ActivityOptionsCompat.makeSceneTransitionAnimation(
                this@FIRST ACTIVITY,
                "EDIT TEXT ID HERE OF FIRST ACTIVITY",
                it
            )
        }
        startActivity(animationIntent, options?.toBundle())
    }

Upvotes: 0

Related Questions