0
votes

I am animating my relative layout with some images in my app. When i write the code to animate the layout on button click, for the first time it is just displaying not animating. Then on the same button click i am sliding up the layout. After that if i click the button sliding down animation works fine.Actually i have to write this sliding down animation inside onCreate itself instead of button click. Given below is my code. Can anyone tell me why it s not showing the animation?

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fadeview);
        iconLayout=(RelativeLayout)findViewById(R.id.icon_ayout);
        iconLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                iconLayout.setVisibility(View.VISIBLE);
                SlideToDown();
            }
        }, 500);
   }

 public void SlideToAbove() {
        Animation slide = null;

        slide=new TranslateAnimation(0.0f, 0.0f, 0.0f,-iconLayout.getHeight());
        slide.setDuration(1000);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        iconLayout.startAnimation(slide);

        slide.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                iconLayout.clearAnimation();

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        iconLayout.getWidth(), iconLayout.getHeight());
                // lp.setMargins(0, 0, 0, 0);
                //lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                iconLayout.setLayoutParams(lp);

            }

        });

    }
 public void SlideToDown() {
        Animation slide = null;

        slide=new TranslateAnimation(0.0f, 0.0f, -iconLayout.getHeight(), 0.0f);

        slide.setDuration(1000);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        iconLayout.startAnimation(slide);

        slide.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                iconLayout.clearAnimation();

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        iconLayout.getWidth(), iconLayout.getHeight());
                lp.setMargins(0,0, 0, 0);
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                iconLayout.setLayoutParams(lp);

            }

        });

    }
1
Have you got the code for the button click? Also are you trying to animate the layout in when activity first created and then animate it out when button is clicked?Andy B

1 Answers

0
votes

This is because you are starting animation in onCreate() and in this time the view is available but it is not actually drawn(doesnt have width and height). Here the value of iconLayout.getHeight() will return value 0.

Solution is to setup a Layoutlistener to the view and the listener will notity you when it is drawn. After that you can start the animation.

ViewTreeObserver loadingObserver = iconLayout.getViewTreeObserver();
loadingObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        iconLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        SlideToDown();
    }
});