Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity Transitions not working

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.

like image 248
Amit Tiwari Avatar asked Jan 28 '16 14:01

Amit Tiwari


People also ask

Which of the following transitions is a shared elements transition?

Customizing Shared Elements Transition In Android L, shared elements transition defaults to a combination of ChangeBounds, ChangeTransform, ChangeImageTransform, and ChangeClipBounds. This works well for most typical cases. However, you may customize this behavior or even define your own custom transition.


2 Answers

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.

like image 91
Abhishek Tiwari Avatar answered Oct 03 '22 10:10

Abhishek Tiwari


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);
        }
like image 26
Zeeshan Shabbir Avatar answered Oct 03 '22 10:10

Zeeshan Shabbir