Home » Android » Prevent certain views from being animated with android:animateLayoutChanges="true"

Prevent certain views from being animated with android:animateLayoutChanges="true"

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a horizontal linear layout that has 3 views. An open and close button and a text view. On load the close button is set to

setVisibility(View.GONE);

So it is only showing one imageview, open, and a textview with some text. I am also using

android:animateLayoutChanges="true"

On my parent layout to animate the textview when it opens and closes. Everything functions right except when I open/close I can see the animation for the two buttons being set to GONE and VISIBLE respectively. I do not want animation on those two ImageViews. Any thoughts on how I can remove the animations on those two views? Thanks in advance.

XML File

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/CuFScrollView"
style="@style/ScrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#A0A0A0"
>
<LinearLayout
    style="@style/LinearLayoutVertical"
    android:paddingTop="20dp"

    >

    <TextView
        android:text="Basic Management Plan for Tactical Field Care"
        android:id="@+id/header"
        style="@style/Header"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#606060"
        android:padding="5dp"
        android:animateLayoutChanges="true"
        >

        <ImageView
            android:id="@+id/open"
            android:src="@drawable/open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="open"
            android:layout_gravity="center_vertical"
            android:animateLayoutChanges="false"
            />

        <ImageView
            android:id="@+id/close"
            android:src="@drawable/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="close"
            android:layout_gravity="center_vertical"
            android:animateLayoutChanges="false"
            />

        <TextView
            android:id="@+id/stepOne"
            android:text="Step 1:"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="0dp"

            style="@style/Bullet"
            />

    </LinearLayout>

</LinearLayout>

Java File

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class Careunderfirestudy extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.careunderfirestudy);

    ImageView close = (ImageView)findViewById(R.id.close);

    close.setVisibility(View.GONE);

    }

public void open(View view){
    TextView stepOne = (TextView)findViewById(R.id.stepOne);
    stepOne.setText("Casualties should be extricated from burning vehicles or buildings and moved to places of relative safety. Do what is necessary to stop the burning process.");
    ImageView close = (ImageView)findViewById(R.id.close);
    ImageView open = (ImageView)findViewById(R.id.open);
    close.setVisibility(View.VISIBLE);
    open.setVisibility(View.GONE);
}

public void close(View view){
    TextView stepOne = (TextView)findViewById(R.id.stepOne);
    stepOne.setText("Step 1:");
    ImageView close = (ImageView)findViewById(R.id.close);
    ImageView open = (ImageView)findViewById(R.id.open);
    close.setVisibility(View.GONE);
    open.setVisibility(View.VISIBLE);
}

}

How to&Answers:

You could just use 1 button and change the image:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#606060"
    android:padding="5dp"
    android:animateLayoutChanges="true"
    android:id="@+id/parent"
    >
    <ImageView
            android:id="@+id/toggleStep"
            android:src="@drawable/open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="toggle"
            android:layout_gravity="center_vertical"
            />

protected void onCreate(Bundle savedInstanceState) {
    ...

    LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
    parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

    ...
}

private boolean mOpen = false;

public void toggle(View view){
        TextView stepOne = (TextView)findViewById(R.id.stepOne);
        ImageView toggle = (ImageView)findViewById(R.id.toggleStep);

        if(mOpen){
            stepOne.setText("Step 1:");
            toggle.setImageResource(R.drawable.open);
        }
        else{
            stepOne.setText("Casualties should be extricated from burning vehicles or buildings and moved to places of relative safety. Do what is necessary to stop the burning process.");
            toggle.setImageResource(R.drawable.close);
        }
        mOpen = !mOpen;
    }

Answer:

The question was: Prevent certain views from being animated with android:animateLayoutChanges=“true” and I don’t see the answer to this question in the previous answer.

I had the same problem and I solved it disabling temporary the LayoutTransition for appearing and disappearing type.

Here examples.

Hide a View without animating it:

LayoutTransition lt = ((ViewGroup)view.getParent()).getLayoutTransition();
lt.disableTransitionType( LayoutTransition.DISAPPEARING );
view.setVisibility(View.GONE);
lt.enableTransitionType( LayoutTransition.DISAPPEARING );

Show a View without animating it:

LayoutTransition lt = ((ViewGroup)view.getParent()).getLayoutTransition();
lt.disableTransitionType( LayoutTransition.APPEARING );
view.setVisibility(View.VISIBLE);
lt.enableTransitionType( LayoutTransition.APPEARING );

This does not prevent other views to be animated consequently to the view visibility change (they are animated under LayoutTransition.CHANGE_APPEARING and LayoutTransition.CHANGE_DISAPPEARING transition type).

Not null check to LayoutTransition may be done for safety.