How to show and hide a view with a slide up and down Android animation

In this android programming source code example, we are going to show and hide a view with a slide up and down animation.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to show and hide a view with a slide up and down animation.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".animation.Animation2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Swipe UP/Swipe Down"
        android:textSize="24sp"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/notebook"
        />

    <Button
        android:id="@+id/slide_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slide Up"/>

    <Button
        android:id="@+id/slide_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slide Down"/>

</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;


public class Animation2 extends AppCompatActivity {

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

        Button slideUp = findViewById(R.id.slide_up);
        Button slideDown = findViewById(R.id.slide_down);
        final ImageView imageView = findViewById(R.id.image);

        slideUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
                imageView.startAnimation(animation);
            }
        });

        slideDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
                imageView.startAnimation(animation);
            }
        });
    }
}

anim/slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="0.0" />
</set>

anim/slide_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment