Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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);
}

}

like image 372
th3ramr0d Avatar asked Jun 22 '15 19:06

th3ramr0d


1 Answers

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.

like image 120
ARLabs Avatar answered Sep 30 '22 12:09

ARLabs