How to add animation to a linear layout?

I am using a linear layout to receive a message with an image from a.net server.

when a new message appears, the layout position increases, and the new message is added to the top of the layout one at a time.

the problem is when a new message arrives, the new message will be added to the layout suddenly.

I want to apply animation to the layout and make my application look like when a new message appears, the message is slowly added to the layout. means previous messages are moving slowly and a new message is being added over the layout.

+3
source share
1 answer

android: animateLayout LinearLayout, . . , . , .

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseLL"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

    <!-- button used to add data -->
    <Button
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Add Content"
        android:onClick="onAddContentClick" />

    <!-- button used to remove data -->
    <Button
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Remove Content"
        android:onClick="onRemoveContentClick" />

    <!-- data will be added to this LinearLayout at run time -->
    <LinearLayout
        android:id="@+id/dataLL"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:animateLayoutChanges="true"
    >

    </LinearLayout>

</LinearLayout>

basicanimation.java

package com.test.animation.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class BasicAnimationActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onAddContentClick(View v) {
        LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
        int dataCount = dataLL.getChildCount();
        TextView newDataTV = generateData(dataCount);
        dataLL.addView(newDataTV, 0);
    }

    public void onRemoveContentClick(View v) {
        LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
        if (dataLL.getChildCount() > 0) {
            dataLL.removeViewAt(0);
        }
    }

    private TextView generateData(int dataCount) {
        TextView TV = new TextView(this);
        TV.setText("Data " + dataCount);
        TV.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        return TV;
    }
}
+4

All Articles