Slidedown and slideup layout with animation
Create two animation xml under res/anim folder
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
Load animation Like bellow Code and start animation when you want According to your Requirement
//Load animation
Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_down);
Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
// Start animation
linear_layout.startAnimation(slide_down);
Sliding UP and DOWN Animation for my Layout
Use startAnimation()
instead of setAnimation()
.
Try this:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
first.startAnimation(slide_up);
second.startAnimation(slide_down);
}
});
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="0%"
android:toYDelta="-200%" />
</set>
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="0%"
android:toYDelta="200%" />
</set>
Here is a good tutorial about different types of animation.
Hope this will help~
Show and hide a View with a slide up/down animation
With the new animation API that was introduced in Android 3.0 (Honeycomb) it is very simple to create such animations.
Sliding a View
down by a distance:
view.animate().translationY(distance);
You can later slide the View
back to its original position like this:
view.animate().translationY(0);
You can also easily combine multiple animations. The following animation will slide a View
down by its height and fade it in at the same time:
// Prepare the View for the animation
view.setVisibility(View.VISIBLE);
view.setAlpha(0.0f);
// Start the animation
view.animate()
.translationY(view.getHeight())
.alpha(1.0f)
.setListener(null);
You can then fade the View
back out and slide it back to its original position. We also set an AnimatorListener
so we can set the visibility of the View
back to GONE
once the animation is finished:
view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});
(android) view shall slide down and after a few seconds automatically slide up
Thx
I did it this way:
public void slideDown() {
textView.animate().translationY(100).setDuration(500);
textView.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 3000ms
textView.animate().translationY(0).setDuration(300);
}
},3000);
}
How to build Animator slide up/down in XML for android?
Make a folder anim in the res folder of the project.
Now add slide_up.xml for slide_up animation.
Then add slide_down.xml for slide down animation.
Code for slide_down.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toYDelta="-1000" android:duration="1500"/>
</set>
Code for slide_up.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toYDelta="1000" android:duration="1500"/>
</set>
Then load the animation in onCreate method consequently:
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
To start it attach it to object you want to be animated:
ImageView img = (ImageView) findViewById(R.id.img);
img.startAnimation(slideUp);
Hope I've helped you. :-)
Slide up/down layout on click
Well, first of all android:animateLayoutChanges
effects the child elements. So, obviously, if you are changing the properties of the element itself, it will not be animated.
I think you can accomplish what you are trying to in API 16 by enabling the LayoutTransition.CHANGING
animation.
<LinearLayout android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Some text to show/hide"/>
</LinearLayout>
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
View text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View message = findViewById(R.id.message);
ViewGroup.LayoutParams params = message.getLayoutParams();
params.height = params.height == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
message.setLayoutParams(params);
}
});
Related Topics
To Draw an Underline Below the Textview in Android
How to Set Toolbar Text and Back Arrow Color
Android On-Screen Keyboard Auto Popping Up
Can't Put Double Sharedpreferences
Decoding Raw H264 Stream in Android
How to Apply Corner Radius to Linearlayout
How to Move Main Content with Drawer Layout Left Side
Should Accessing Sharedpreferences Be Done Off the UI Thread
Failure [Install_Failed_Update_Incompatible] Even If App Appears to Not Be Installed
Get Response Status Code Using Retrofit 2.0 and Rxjava
Onnewintent() Lifecycle and Registered Listeners
Populating Spinner Directly in the Layout Xml
Layout Weights Do Not Work Inside a Scrollview
Writing/Reading Files To/From Android Phone's Internal Memory
The Specified Child Already Has a Parent. You Must Call Removeview() on the Child's Parent First
Which View Should Be Used for New Material Design Bottom Navigation