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>
AstroCB
  • 12,337
  • 20
  • 57
  • 73
jason
  • 3,932
  • 11
  • 52
  • 123
  • 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. – nilsi Nov 23 '15 at 04:51
  • 1
    Was this issue ever resolved? I am having a similar problem. – Brian Jul 06 '16 at 20:36

4 Answers4

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

Cletus Ajibade
  • 1,550
  • 15
  • 14
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>
  • 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](https://github.com/lgvalle/Material-Animations), and it seems to say that you can set up the transition just in Java. Am I misunderstanding? –  Jan 25 '16 at 01:01
  • 14
    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). – Brian Jul 06 '16 at 20:34
  • 2
    this is not the answer for given question – abh22ishek Nov 16 '16 at 09:53
  • @Brian do you have an example of using the Transitions API that works? I tried following this https://developer.android.com/training/transitions/start-activity but couldnt get it working. – Etienne Lawlor Jun 19 '18 at 19:49
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

Boy
  • 7,010
  • 4
  • 54
  • 68
  • What are you passing for your test view , when you are in activity or in fragment? – Stoycho Andreev Jul 25 '16 at 10:01
  • 1
    @Sniper I missed a builder without transitioning views, which I was pointed to in another question: ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(TourAndLoginActivity.this); is enough – Boy Jul 25 '16 at 12:20
  • aha I see your point, thanks about that but ActivityOptionsCompat is mandatory to have this new transitions – Stoycho Andreev Jul 25 '16 at 14:41
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>
nilsi
  • 10,351
  • 10
  • 67
  • 79