Android UI Tutorial - Android LayoutTransition








The following code shows how to Use LayoutTransition.

Example

Main layout xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="19dp"
        android:onClick="onClick"
        android:text="Button" />

</LinearLayout>

Menu main.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_settings"
        android:title="menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never" />
</menu>

Main Activity Java code

import android.animation.LayoutTransition;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/*from w  w w .j  a va  2s .co m*/
public class MainActivity extends Activity {

  private ViewGroup viewGroup;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LayoutTransition l = new LayoutTransition();
    l.enableTransitionType(LayoutTransition.CHANGING);
    viewGroup = (ViewGroup) findViewById(R.id.container);
    viewGroup.setLayoutTransition(l);

  }

  public void onClick(View view) {
    viewGroup.addView(new Button(this));
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }
}
null