Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate changing LayoutParams (RelativeLayout)

when I click on a RelativeLayout it change the size. How can I make this change animated? So it looks like the rectangle grows to full screen?

My Layout:

<RelativeLayout 
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"
android:background="#ff000000">

<RelativeLayout
    android:id="@+id/srLayout"
    android:layout_width="62.5dp"
    android:layout_height="132.5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:onClick="sleepRoom"
    android:background="#ffffffff"></RelativeLayout>

</RelativeLayout>

The code:

public void sleepRoom(View view) {
    RelativeLayout sr = (RelativeLayout) findViewById(view.getId());
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sr.getLayoutParams();
    params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
    params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    sr.setLayoutParams(params);
}
like image 508
Laire Avatar asked Apr 27 '15 15:04

Laire


1 Answers

Simple trick that worked for me:

Adding:

android:animateLayoutChanges="true"

to the xml of the view whose layoutParams you are changing.

Then, adding this line of code in your Java/Kotlin code before changing the params:

Java:

((ViewGroup) sr).getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

Kotlin:

(sr as ViewGroup).layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
like image 170
Rezwan Azfar Haleem Avatar answered Sep 24 '22 19:09

Rezwan Azfar Haleem