I've been looking through many google results / questions on here to determine how to show/hide a view via a vertical animation, but I can't seem to find one that's exactly right or not too vague.
I have a layout (undo bar) that's below another layout and above multiple other widgets; this undo bar should vertically slide open and slide closed, depending on the circumstances.
Currently all I do right now is set the view to be visible or gone.
Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.
use setVisibility method. for hiding button you can use button. setVisibility(View. GONE);.. you can use View.
Set the attribute android:animateLayoutChanges="true"
inside the parent layout .
Put the view in a layout if its not and set android:animateLayoutChanges="true"
for that layout.
NOTE: This works only from API Level 11+ (Android 3.0)
I created an extension for RelativeLayout
that shows/hides Layouts with animations. It can extend any kind of View
to gain these features.
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.widget.RelativeLayout; public class AnimatingRelativeLayout extends RelativeLayout { Context context; Animation inAnimation; Animation outAnimation; public AnimatingRelativeLayout(Context context) { super(context); this.context = context; initAnimations(); } public AnimatingRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initAnimations(); } public AnimatingRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initAnimations(); } private void initAnimations() { inAnimation = (AnimationSet) AnimationUtils.loadAnimation(context, R.anim.in_animation); outAnimation = (Animation) AnimationUtils.loadAnimation(context, R.anim.out_animation); } public void show() { if (isVisible()) return; show(true); } public void show(boolean withAnimation) { if (withAnimation) this.startAnimation(inAnimation); this.setVisibility(View.VISIBLE); } public void hide() { if (!isVisible()) return; hide(true); } public void hide(boolean withAnimation) { if (withAnimation) this.startAnimation(outAnimation); this.setVisibility(View.GONE); } public boolean isVisible() { return (this.getVisibility() == View.VISIBLE); } public void overrideDefaultInAnimation(Animation inAnimation) { this.inAnimation = inAnimation; } public void overrideDefaultOutAnimation(Animation outAnimation) { this.outAnimation = outAnimation; } }
You can override the original Animation
s using overrideDefaultInAnimation
and overrideDefaultOutAnimation
My original Animations were fadeIn/Out, I am adding XML animation files for Translating in/out of the screen (Translate to top and from top)
in_animation.xml:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="600" android:fillAfter="false" android:fromXDelta="0" android:fromYDelta="-100%p" android:toXDelta="0" android:toYDelta="0" />
out_animation.xml:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="600" android:fillAfter="false" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="-100%p" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With