Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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