0

I want to click the Button it will show the TextViewis visible(perform slidedown animation), then want to click the button again it will perform another animation(slideup). after that don't need to show the TextView.
How do i fix it?
please anyone have an answer to help me.

bclickss.setOnClickListener(new View.OnClickListener() {
        boolean visible;
        @Override
        public void onClick(View v) {
            if( visible = !visible) {
            tv2.setVisibility(visible ? View.VISIBLE : View.GONE);
            Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
            tv2.startAnimation(anim);
                }
            else {
                Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
                tv2.startAnimation(anim);
                tv2.setVisibility(View.GONE);
            }
            }
    });
ADM
  • 20,406
  • 11
  • 52
  • 83
Madhuslin KS
  • 445
  • 1
  • 6
  • 16
  • Possible duplicate of [How to hide view when animation done in android?](https://stackoverflow.com/questions/3223867/how-to-hide-view-when-animation-done-in-android) – ADM Apr 05 '19 at 11:14

5 Answers5

2

use this before startAnimation

if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()

anim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        tv2.setVisibility(View.VISIBLE);
    }
    @Override
    public void onAnimationEnd(Animation animation) {
      tv2.setVisibility(View.GONE);
    }
    @Override
    public void onAnimationRepeat(Animation animation) {}
  });
  tv2.startAnimation(anim);

In second animation you need something like this:

if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()

anim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
      tv2.setVisibility(View.VISIBLE);
    }
    @Override
    public void onAnimationEnd(Animation animation) {
    }
    @Override
    public void onAnimationRepeat(Animation animation) {}
  });
  tv2.startAnimation(anim);
Eugene Babich
  • 1,271
  • 15
  • 25
1
try this:
   //You can add Animation listener to your animation object like

    anim .setAnimationListener(new Animation.AnimationListener(){
        @Override
        public void onAnimationStart(Animation arg0) {
        }
        @Override
        public void onAnimationRepeat(Animation arg0) {
        }
        @Override
        public void onAnimationEnd(Animation arg0) {

     tv2.setVisibility(View.GONE);

        }
    });
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
1

You should use clearAnimation before setting the visibility to View.GONE

anim .setAnimationListener(new Animation.AnimationListener(){
    @Override
    public void onAnimationStart(Animation arg0) {
    }
    @Override
    public void onAnimationRepeat(Animation arg0) {
    }
    @Override
    public void onAnimationEnd(Animation arg0) {
         tv2.clearAnimation();
         tv2.setVisibility(View.GONE);

    }
});
Ferran
  • 1,442
  • 1
  • 6
  • 10
0

I also have the same issue. Please follow below lines:

Animation slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
Animation slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);

for making view visible :

 view.setVisibility(View.VISIBLE);
 view.startAnimation(slide_down);

for hiding it:

view.startAnimation(slide_up);
view.setVisibility(View.GONE);

If works please accept the answer.

Bipin Tiwari
  • 320
  • 3
  • 13
0

I have one suggestion for you to use YOYO Animation lib for android. It provide verious animations along with call back methods so you can hide your textview once your animation get finished in call back

check here