Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate LayoutParams changes in ConstraintLayout

I am creating app in which there is screen with searchBar by this lib. My activity layout looks like this:

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

<android.support.constraint.ConstraintLayout
        android:id="@+id/wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true">

    <TextView
            android:id="@+id/title"
            android:textColor="@color/colorPrimary"
            android:text="@string/mainScreenTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:paddingTop="100dp"/>

    <com.arlib.floatingsearchview.FloatingSearchView
            android:id="@+id/searchView"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginTop="100dp"
            android:animateLayoutChanges="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title"/>

</android.support.constraint.ConstraintLayout>

</RelativeLayout>

There i have to move FloatingSearchingView to top of screen and change height to for example 500dp - like this:

app:layout_constraintTop_toTopOf="parent"
android:layout_height="500dp"

And later i will have to move it back and change height again to default value

app:layout_constraintTop_toBottomOf="@+id/title"
android:layout_height="50dp"

The problem is, i have to animate it. I have spent a lot of time searching for solution but unfortunately i didnt find anything.

like image 694
Qiteq Avatar asked May 08 '17 15:05

Qiteq


People also ask

Is ConstraintLayout better than RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

Is ConstraintLayout faster than LinearLayout?

Results show that the fastest layout is Relative Layout, but difference between this and Linear Layout is really small, what we can't say about Constraint Layout. More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.


1 Answers

I had to animate the TextView height inside a ConstraintLayout.

 <androidx.constraintlayout.widget.ConstraintLayout
                ...
                android:animateLayoutChanges="true"
                >

                <TextView
                 ...

I first informed the ConstraintLayout that the layout was about to change:

constraintlayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

and then updated the textView height (or whatever other param)

textView.getLayoutParams().height = 200;
textView.requestLayout();

for something simple as that, no need to set animators or involve other libraries.

like image 103
Alberto M Avatar answered Sep 24 '22 11:09

Alberto M