Activity Transitions not working
Asked Answered
T

2

17

I am trying to implement Activity Transitions but I am not able to see the effects. Here is the code for my first activity:

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

private void setUpWindowAnimations() {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Log.i("ANIM", "Fade called");
            Fade fade = new Fade(2);
            fade.setDuration(3000);
            getWindow().setExitTransition(fade);
        }
    }

Here is the code for second activity:

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

private void setUpWindowAnimations() {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Log.i("ANIM", "slide called");
            Slide slide = new Slide(Gravity.LEFT);
            slide.setDuration(3000);
            getWindow().setEnterTransition(slide);
        }
    }

Even though I have set Fade out animation, there is no fading, also, Slide works in default way, i.e. the direction is BOTTOM instead of LEFT.

Here is my values/style.xml and here is my v21/styles.xml.

Here is my AndroidManifest.xml:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

Why are these transitions not working and how to make them work. I used paste.ubuntu.com because the SO editor was not showing xml properly.

Transubstantiate answered 28/1, 2016 at 14:36 Comment(6)
Did you define a style in styles.xml with your custom transition?Smell
@Smell No, I haven't. You can check both my styles.xml, I have put a link.Transubstantiate
The paste.ubuntu link you put doesn't work.Smell
@Smell styles.xml and v21/styles.xmlTransubstantiate
Was this ever resolved? I am having a similar problem.Vada
yes, it works on lollipop above.Transubstantiate
A
44
Bundle bundle = ActivityOptions.makeSceneTransitionAnimation(this).toBundle();
this.startActivity(intent,bundle);

Add these two lines after you setup your intent between two activities, this will work.

You cannot just start an activity via startActivity(intent) method, you need to specify transitions across activies using bundles.

Act answered 6/4, 2016 at 12:37 Comment(0)
E
1

Declare setUpWindowAnimations(); before setContentView.

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

    }

private void setUpWindowAnimations() {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Log.i("ANIM", "Fade called");
            Fade fade = new Fade(2);
            fade.setDuration(3000);
            getWindow().setExitTransition(fade);
        }
    }

Other Solution

make a xmlTransition and put this xml code there

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@android:interpolator/accelerate_decelerate">
    <fade android:fadingMode="fade_out"/>
    <slide android:slideEdge="bottom"/>
</transitionSet>

This should be your Style for Api21

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowTransitionBackgroundFadeDuration">1000</item>
    </style>
</resources>

Then put this code in your activity before setCreateView

if (Build.VERSION.SDK_INT >= 21) {

            TransitionInflater inflater = TransitionInflater.from(this);
            Transition transition = inflater.inflateTransition(R.transition.transition_a);
            getWindow().setExitTransition(transition);
        }

this should be in your other activity before setCreateView

if(Build.VERSION.SDK_INT >= 21){
            Slide slide = new Slide();
            slide.setDuration(1000);
            getWindow().setEnterTransition(slide);
        }
Equilateral answered 28/1, 2016 at 14:46 Comment(3)
it is also not working. I changed the slide direction to left and still it is sliding from bottom.Transubstantiate
Why does it need to be before setCreateView??Smell
are you testing it on 21> device?Equilateral

© 2022 - 2024 — McMap. All rights reserved.