0

I have a problem with the animations, here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new Handler().postDelayed(new Runnable(){

        @Override
        public void run() {
            Animation anim= new TranslateAnimation(0, 0, 0, -200);
            anim.setDuration(2000);
            anim.setFillAfter(true);
            findViewById(R.id.button1).startAnimation(anim);

        }

    }, 2000);
}

And the main layout is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:text="Button" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:minHeight="120sp"/>

<Button 
    android:id="@+id/button2" 
    android:layout_width="fill_parent" 
    android:text="Button" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/button1" 
    android:minHeight="120sp"/>

</RelativeLayout>

I want the button2 move together with the button1. I know I can apply the same animation to both buttons and they move together but then the button2 will move away to the bottom margin.

Does somebody know how can I fix it?

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Addev
  • 31,819
  • 51
  • 183
  • 302

2 Answers2

2

I'm not sure of what you really want to achieve. I guess you would like button2 to remain at the top of your layout after the animation. Am I right?

As far as I know, in Android, views don't reposition themselves when animating other views. They do reposition when setting a view's visibility to GONE.

Therefore you could:

  1. Apply a different animation to button2 in order to move it where you want it to be.
  2. Set visibility of button1 to gone after the animation:

    button1.setVisibility( View.GONE );
    
Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • Thanks for the answers. So if i want the button 2 expand until fill the whole window I should combine a translate and a vertical zoom? and then put the button 1 to gone right? – Addev Aug 01 '11 at 15:26
  • With your code, if you just set button1 to GONE, button2 expands and fills the entire layout because of the android:layout_height="fill_parent" attribute. So just set button1 to GONE after the animation. – Xavi Gil Aug 01 '11 at 15:30
  • yes but I want to animate the gone moment translating the button 1 up – Addev Aug 01 '11 at 18:03
  • It is not that easy. [Here](http://stackoverflow.com/questions/2634073/how-do-i-animate-view-setvisibilitygone) you have a related question. In your case I would recommend to use a different animation for button2. Hope it helps. – Xavi Gil Aug 01 '11 at 18:20
2

Firstly, Android animations don't affect layout. Your button hasn't really moved, it just looks like it has.

Secondly, if you want both buttons to move together, put them in a linear layout and animate that instead.

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86