Animations not working

Search This thread

Chromium

Senior Member
Oct 5, 2012
4,481
6,524
Toronto
chromium1.blogspot.ca
Ive got a checkbox which should set a textview visible while checked, and hide it when its not checked. I have also created two basic animations that should appear during hiding and showing of the textview (fadeIn and fadeOut). Heres my onclicklistener for the checkbox:

Code:
checkBox1.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        fadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
        fadeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out);
        
        if (((CheckBox) v).isChecked()) {
            
            fadeIn.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
                
                @Override
                public void onAnimationEnd(Animation animation) {
                    textView1.setVisibility(View.VISIBLE);
                }
                
            });
            textView1.startAnimation(fadeIn);
            
        }
        else {
            
            fadeOut.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
                
                @Override
                public void onAnimationEnd(Animation animation) {
                    textView1.setVisibility(View.GONE);
                }
                
            });
            textView1.startAnimation(fadeOut);
        }
    }
});

And here are my two animations:

Code:
<!-- fade_in.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fillAfter="true"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />

Code:
<!-- fade_out.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fillAfter="true"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" />

My problem is that the fade animation only shows when I uncheck the checkbox (hide the textbox). When I try to check the box (show the textview) it just appears without showing the animation. Any help would be appreciated. Thanks in advance.

Edit: Had to move "textView1.setVisibility(View.VISIBLE);" into the onAnimationStart method.
 
Last edited:

GRYMALA

Member
May 5, 2014
19
5
Try this:

Code:
checkBox1.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        fadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
        fadeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out);
        
        if (((CheckBox) v).isChecked()) {
            

            fadeIn.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
                
                @Override
                public void onAnimationEnd(Animation animation) {
                    textView1.setVisibility(View.VISIBLE);
                }
                
            });

            textView1.setAlpha(0);
            textView1.setVisibility(View.VISIBLE);
            textView1.startAnimation(fadeIn);
            
        }
        else {
            
            fadeOut.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
                
                @Override
                public void onAnimationEnd(Animation animation) {
                       textView1.setVisibility(View.INVISIBLE);
                }
                
            });
            textView1.startAnimation(fadeOut);
        }
    }
});