Android transition animation is not working
Asked Answered
C

4

9

Android transition is same for explode and slide.Actually I don't think its animating. Also duration is not 6 seconds. How can I fix it?

Code taken from here.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

        Transition ts = new Slide();  //Slide(); //Explode();

        ts.setDuration(6000);
        getWindow().setEnterTransition( ts );
        getWindow().setExitTransition( ts );
        setContentView(R.layout.activity_main_activity);



    }




    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishAfterTransition();
    }

Style-v21.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="BaseAppTheme" parent="android:Theme.Material.Light">
        <item name="android:colorControlHighlight">#0000AA</item>
        <item name="android:navigationBarColor">#0000AA</item>
        <item name="android:colorPrimaryDark">#0000AA</item>
        <item name="android:colorPrimary">#0000FF</item>
        <item name="android:colorAccent">#00FF00</item>
        <item name="android:windowBackground">@android:color/black</item>
        <item name="android:textColorPrimary">@android:color/white</item>

        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>

        <!-- specify enter and exit transitions -->
        <item name="android:windowEnterTransition">@transition/explode</item>
        <item name="android:windowExitTransition">@transition/explode</item>
    </style>
</resources>
Cayla answered 24/3, 2015 at 23:10 Comment(2)
I have this problem as well! Even though the accepted answer solves the problem I would like to know why your code is not working.Hook
Was this issue ever resolved? I am having a similar problem.Gwenn
I
5

You can use this to start a new activity with transition

startActivity(new Intent(this, NewActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

Create file res/anim/slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
     <translate android:duration="1000" android:fromXDelta="100%" android:toXDelta="0%" />
    <alpha android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

Create file res/anim/slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
     <translate android:duration="2000" android:fromXDelta="0%" android:toXDelta="-100%"/>
     <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
Ichthyosis answered 30/3, 2015 at 11:52 Comment(4)
So what's the setExitTransition(ts) do, if you still have to explicitly reference the xml file when changing activities? I've been following this, and it seems to say that you can set up the transition just in Java. Am I misunderstanding?Uniformed
This is a poor answer that does not use the newer Transitions API (the API used in the question and the next-generation API for all future activity transitions).Gwenn
this is not the answer for given questionCarinacarinate
@Gwenn do you have an example of using the Transitions API that works? I tried following this developer.android.com/training/transitions/start-activity but couldnt get it working.Starks
D
18

In my own case, for some reason, Transition Animation was off in the developer options of my android device.

Just go to Settings -> Developer Options -> Transition animation scale: set to Animation scale 1x

Distribute answered 28/2, 2016 at 22:20 Comment(1)
someone give this guy a medal and donut !!Kapor
I
5

You can use this to start a new activity with transition

startActivity(new Intent(this, NewActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

Create file res/anim/slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
     <translate android:duration="1000" android:fromXDelta="100%" android:toXDelta="0%" />
    <alpha android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

Create file res/anim/slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
     <translate android:duration="2000" android:fromXDelta="0%" android:toXDelta="-100%"/>
     <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
Ichthyosis answered 30/3, 2015 at 11:52 Comment(4)
So what's the setExitTransition(ts) do, if you still have to explicitly reference the xml file when changing activities? I've been following this, and it seems to say that you can set up the transition just in Java. Am I misunderstanding?Uniformed
This is a poor answer that does not use the newer Transitions API (the API used in the question and the next-generation API for all future activity transitions).Gwenn
this is not the answer for given questionCarinacarinate
@Gwenn do you have an example of using the Transitions API that works? I tried following this developer.android.com/training/transitions/start-activity but couldnt get it working.Starks
S
4

I found out that I need to really do

ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, test, "transition_name");
ActivityCompat.startActivity(TourAndLoginActivity.this, new Intent(TourAndLoginActivity.this, LoginActivity.class), activityOptionsCompat.toBundle());

Still puzzling, but I don't know why we need to have a shared object to get the enter / exit transition. If I supply null instead of the ActivityOptionsCompat, there is no transition

Spoonerism answered 3/6, 2016 at 6:26 Comment(3)
What are you passing for your test view , when you are in activity or in fragment?Rand
@Sniper I missed a builder without transitioning views, which I was pointed to in another question: ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(TourAndLoginActivity.this); is enoughSpoonerism
aha I see your point, thanks about that but ActivityOptionsCompat is mandatory to have this new transitionsRand
H
3

Don't forget to apply the theme for the activity or the whole package in your AndroidManifest.xml.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.package"
android:theme="@style/BaseAppTheme">
    ....
    ....
</manifest>
Hook answered 23/11, 2015 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.