Reputation: 195
I have created a button that I want to translate down whenever it is pressed on. I created an xml file in the anim folder to set a downward translation.
The translation works but the button reverts back to its original position when the animation finishes. To counter this problem, I used the code:
translationDown.setFillAfter(true);
translationDown.setFillEnabled(true);
Although the button stayed in its position after it finished the translate animation, it stopped working and won't translate down from that position when it is pressed again.
How can I make the button translate down every time it is pressed?
EDIT:
As requested, here is the code for the translation, which is stored in translate_down.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="300"
android:duration="250"
/>
</set>
This is the code in my java file:
final Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_down);
relativeLayoutButtons.startAnimation(slideDown);
Upvotes: 2
Views: 1737
Reputation: 577
This is because the animation uses a representation (bitmap copy) of your view (i.e. your button). When the animation is in progress, the bitmap copy, and not your view is getting translated. After the animation is completed, your actual view has not moved.
Using translationDown.setFillAfter(true) and translationDown.setFillEnabled(true) will not help because these are for animation chaining i.e. if you want to continue the animation with another animation object.
What you need to do is set the position properties of your view after the animation. Here is an example of how that can be accomplished:
final Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_down);
relativeLayoutButtons.startAnimation(slideDown);
relativeLayoutButtons.getAnimation().setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// Set the position properties of your view here.
// e.g.
relativeLayoutButtons.setTranslationX(50);
relativeLayoutButtons.setTranslationY(50);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
Upvotes: 1
Reputation: 1459
Old Animation API is just move view's drawing but not move actual view position. So, pressing translated view does not responding (view is still in original position actually.)
Use new Animator API. This API moves actual view position.
view.animate().translationY(300).setDuration(250).start();
See this google's blog post for introduction.
Upvotes: 0
Reputation: 11497
Well, my bet would be for you to use ontouch()
instead of onClick()
. Once you change the Button
position, I think it's only clickable on the original position.
Try something like the following.
Button bt = (Button) findViewById(R.id.button);
bt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event){
switch(event.getAction())
{
case MotionEvent.ACTION_UP :
{
// Do whatever you want here.
}
return true;
}});
Upvotes: 0