Saturday, January 28, 2017

android - Slide in animation example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e7ece3"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slide In Animation"
        android:layout_margin="25dp"
        android:layout_gravity="top|center_horizontal"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sample TextView"
        android:textSize="25sp"
        android:visibility="invisible"
        android:gravity="center_horizontal"
        android:textColor="#ff0000"
        android:layout_margin="35dp"
        app:layout_anchor="@id/btn"
        android:layout_gravity="bottom|center_horizontal"
        app:layout_anchorGravity="bottom|center_horizontal"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Slide;
import android.transition.TransitionManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private Button mButton;
    private TextView mTextView;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButton = (Button) findViewById(R.id.btn);
        mTextView = (TextView) findViewById(R.id.tv);

        // Initialize a new click listener for button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    android.transition.Slide
                        This transition tracks changes to the visibility of target views in the
                        start and end scenes and moves views in or out from one of the edges of
                        the scene. Visibility is determined by both the setVisibility(int) state
                        of the view as well as whether it is parented in the current view hierarchy.
                        Disappearing Views are limited as described in
                        onDisappear(android.view.ViewGroup, TransitionValues, int, TransitionValues, int).
                */
                // Call requires API level 21
                Slide slide = new Slide();

                /*
                    setSlideEdge(int slideEdge)
                        Change the edge that Views appear and disappear from.
                */
                slide.setSlideEdge(Gravity.LEFT); // Slide in left
                //slide.setSlideEdge(Gravity.TOP); // Slide in top
                //slide.setSlideEdge(Gravity.RIGHT); // Slide in right
                //slide.setSlideEdge(Gravity.BOTTOM); // Slide in bottom

                /*
                    beginDelayedTransition(ViewGroup sceneRoot, Transition transition)
                        Convenience method to animate to a new scene defined by all changes within
                        the given scene root between calling this method and the next rendering frame.
                */
                TransitionManager.beginDelayedTransition(mCLayout,slide);
                mTextView.setVisibility(View.VISIBLE);
            }
        });
    }
}