I set up a simple activity to try to boil down this problem. I have a FrameLayout in which I have enabled "AnimateLayoutchanges". I have a button which onclick changes the layout margin and width of a TextView. If this is the only layout change, the change in margin and width will NOT get animated. However, if I simple set the visibility if another child of the same FrameLayout, both the visibility change AND the margin/width change will animate.
Side note: the visibility and margin+width change will happen in sequence as opposed to simultaneously, is there a way to change this behavior?
<LinearLayout 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:background="#888"
android:orientation="vertical"
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=".activities.TestActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/PivotTheme" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonHandler"
android:text="test button 1" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/white">
<TextView
android:id="@+id/testTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/purple_gradient_end"
android:text="TestView" />
<TextView
android:id="@+id/testTv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@color/purple_gradient_end"
android:text="TestView" />
</FrameLayout>
</LinearLayout>
Activity:
public void buttonHandler(View view){
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) testTv.getLayoutParams();
if(testDown) {
params.setMargins(100, 400, 0, 0);
params.width = 400;
testTv2.setVisibility(View.GONE);// if I comment out this line, nothing will animate
testDown = false;
} else {
params.setMargins(0, 0, 0, 0);
params.width = 100;
testTv2.setVisibility(View.VISIBLE);
testDown = true;
}
testTv.setLayoutParams(params);
}
I feel like this is a bug with android where changes in margin or width are failing to set a flag to trigger animation, but if this flag is set by changing visibility, then all the animations are properly handled.