Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Animations and Transitions The Transitions Framework Transitioning Back and Forth

saikiran maddukuri
saikiran maddukuri
17,919 Points

not transition back and forth on personal code please help

Hi i tried it on with my own code in such a way that it shuts up and down when the floating Action Button was clicked but problem is it is shutting up but not shutting down i haven't used Butterknife api please help the transition is not moving back and forth MainActivity.java

package com.teamtreehouse.android.gem;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.ChangeBounds;
import android.transition.Fade;
import android.transition.Scene;
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {
    FloatingActionButton floatingActionButton;
    TransitionManager mTransitionManager;
    Scene currentScene;
    Scene expandedScene;
    Scene collapsedScene;

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

        floatingActionButton=findViewById(R.id.floatingActionButton);
        setUpTransition();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(currentScene == expandedScene){
                    currentScene = collapsedScene;
                }
                else{
                    currentScene=expandedScene;
                }
                mTransitionManager.transitionTo(currentScene);
            }
        });
    }

    private void setUpTransition() {
        mTransitionManager=new TransitionManager();
        ViewGroup vg=findViewById(R.id.container);
        expandedScene= Scene.getSceneForLayout(vg,R.layout.activity,this);
        expandedScene.setEnterAction(new Runnable() {
            @Override
            public void run() {
                floatingActionButton=findViewById(R.id.floatingActionButton);
                currentScene=expandedScene;
            }
        });
        TransitionSet expandedTransitionSet = new TransitionSet();
        expandedTransitionSet.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
        ChangeBounds changeBounds=new ChangeBounds();
        changeBounds.setDuration(1000);
        expandedTransitionSet.addTransition(changeBounds);
        Fade fadein=new Fade();
        fadein.addTarget(R.id.textView2);
        fadein.setDuration(1000);
        expandedTransitionSet.addTransition(fadein);
        collapsedScene=Scene.getSceneForLayout(vg,R.layout.activity_main_expanded,this);
        collapsedScene.setEnterAction(new Runnable() {
            @Override
            public void run() {
                floatingActionButton=findViewById(R.id.floatingActionButton);
                currentScene=collapsedScene;
            }
        });
        TransitionSet collapsedTransitionSet=new TransitionSet();
        collapsedTransitionSet.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
        Fade fadeout= new Fade();
        fadeout.addTarget(R.id.textView2);
        fadeout.setDuration(1000);
        collapsedTransitionSet.addTransition(fadeout);
        ChangeBounds colapseBounds=new ChangeBounds();
        colapseBounds.setDuration(1000);
        collapsedTransitionSet.addTransition(colapseBounds);
        mTransitionManager.setTransition(expandedScene,collapsedScene,collapsedTransitionSet);
        mTransitionManager.setTransition(collapsedScene,expandedScene,expandedTransitionSet);
        collapsedScene.enter();
    }
}

activity_main.html

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/main_title" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="316dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="32dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="8sp"
        android:text="sounds from mars"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</android.support.constraint.ConstraintLayout>

activity_main_expanded.html

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="50sp"
        android:adjustViewBounds="true"
        android:cropToPadding="false"
        android:scaleType="fitXY"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/main_title" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="316dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.741"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="8sp"
        android:text="sounds from mars"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="363dp"
        android:layout_height="97dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/sounds"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>

strings.html

<resources>
    <string name="app_name">Gem</string>
    <string name="sounds">Twinkle twinkle little star how i wonder what you want \n twinkle twinkle little star \n Johny johny yes papa eating shugar no papa</string>
</resources>

3 Answers