Kailash Ahirwar
Kailash Ahirwar

Reputation: 769

Animate(ScaleAnimation) LinearLayout when it's height changes due to add/removal of view inside it

I have a LinearLayout & i have 2 TextViews inside it. Dynamically i want to hide one TextView or i can add TextViews at runtime. My problem is when TextView disappears from LinearLayout it collapses in no time. I want to animate the LinearLayout(ScaleAnimation) when views get removed from LinearLayout.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" >

  <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="#000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:background="#fff"
        android:orientation="vertical"
        android:padding="10dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="Hello, World" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="Hello, World 2" />
    </LinearLayout>  
  </LinearLayout>

  <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:text="Remove TextView" />

</RelativeLayout>

Snapshot of layout... enter image description here

I want to animate LinearLayout when second TextView got removed from LinearLayout & if another TextView got added into LinearLayout. ScaleAnimation is good for the above task but i don't know values required for this type of animation.

Upvotes: 2

Views: 569

Answers (1)

cmsousa
cmsousa

Reputation: 1

Pretty sure that you do not need it anymore, but for future searchers:

For the remove animation, it is possible to make the item invisible using view.setVisibility(View.INVISIBLE) and after the animation (addAnimationListener) effectually remove it from the linear layout. When a view is invisible its parent keeps measuring it.

Upvotes: 0

Related Questions