Home > database >  Add animation for Changing Height of Layout
Add animation for Changing Height of Layout

Time:12-20

I have a small view, which at the beginning only contains a loading indicator, which is why it is relatively small. However, when the actual content is loaded, I would like to display that instead of the loading indicator, for which I add 2 text fields and an ImageView. Currently I just do this by hiding the ProgressBar and showing the elements I just mentioned, but this way there is a hard cut between the two states. I would like to change this so that first the height of the view is adjusted over a short period of time, and then the content is shown (maby faded in, but I'm more concerned about changing the height).

In general I already have some ideas how to do this, but I don't know how to calculate the new height? Is there a way to calculate it, or even a function directly from Android that solves my Problem?

Thanks already :)^

Heres the Layout for the Fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".viewables.fragments.home.HomeFragment">

    <ListView
        android:id="@ id/home_vertretungsplan_preview"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/vertretungsplan_list_item"/>

    <include
        android:animateLayoutChanges="true"
        android:layout_weight="0"
        android:id="@ id/home_article_preview"
        layout="@layout/thomsline_main_recyclerview_article"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
</LinearLayout>

and here ist the Included Layout

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:animateLayoutChanges="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:clickable="true"
    android:focusable="true"
    app:cardElevation="10dp"
    app:cardCornerRadius="20dp"
    app:cardPreventCornerOverlap="false">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:id="@ id/thomsline_post_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="0dp"
            android:adjustViewBounds="true"
            android:padding="0dp"
            android:scaleType="centerCrop"
            android:src="@drawable/img_thomsline_article_image_default"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="20dp"
            android:id="@ id/container1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/thomsline_post_image"
            app:layout_constraintLeft_toLeftOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="19sp"
                android:id="@ id/thomsline_post_title"
                tools:text="Article Title"/>


            <TextView
                android:id="@ id/thomsline_post_excerpt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textAllCaps="false"
                android:textSize="12.5sp"
                tools:text="Post Excerpt" />

        </LinearLayout>

        <ProgressBar
            android:id="@ id/thomsline_post_loading_indicator"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="gone"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

CodePudding user response:

You can try using animateLayoutChanges attribute in your parent layout.

Add this line to your root element of your activity layout :

android:animateLayoutChanges="true"

Or you can use TransitionManager for smooth transitions. (API 19 )

Add this code to your file just before the line where you set your content :

TransitionManager.beginDelayedTransition(cardView); //Default transition

You can pass your preferred transition to this function like this :

Transition transition = new ChangeBounds(); 
TransitionManager.beginDelayedTransition(cardView,transition);

You can choose any transition :

  • ChangeBounds
  • Explode
  • Slide
  • Fade
  • AutoTransition
  • TransitionSet

If you want to know more about animations in android, i would suggest u read this : Simple Animations in Android

  • Related