title: Android: Change parent layout of a button with animation
body: <p>I am writing my first android application using Eclipse. The screen of my application shows a 2x4 table of buttons (made using 4 linear layouts) and I have a method which shuffles the buttons by removing them from one linear layout and adding them to another. I have used android:animateLayoutChanges="true" in my xml but that just caused some of the buttons to blank-out before reappearing in their new spots. Is there any way to get a smooth transition? Thanks.</p>
  
  <p>My xml:
          </p>
  
  <pre><code>&lt;TableLayout
      android:id="@+id/table"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_margin="0dp"
      android:layout_marginStart="0dp"
      android:layout_weight="1" &gt;
  
      &lt;LinearLayout
          android:id="@+id/ll1"
          android:animateLayoutChanges="true"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/button1"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btnorange"
              android:text="" /&gt;
  
          &lt;Button
              android:id="@+id/button2"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btnyellow"
              android:text="" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/ll2"
          android:animateLayoutChanges="true"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/button3"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btnblue"
              android:text="" /&gt;
  
          &lt;Button
              android:id="@+id/button4"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btngreen"
              android:text="" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/ll3"
          android:animateLayoutChanges="true"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/button5"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btnpink" /&gt;
  
          &lt;Button
              android:id="@+id/button6"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btncyan"
              android:text="" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/ll4"
          android:animateLayoutChanges="true"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/button7"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btnred"
              android:text="" /&gt;
  
          &lt;Button
              android:id="@+id/button8"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="2dp"
              android:layout_marginLeft="2dp"
              android:layout_marginRight="2dp"
              android:layout_marginTop="2dp"
              android:layout_weight="1"
              android:background="@drawable/btnviolet"
              android:text="" /&gt;
      &lt;/LinearLayout&gt;
  
  
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>My Activity class:</p>
  
  <pre><code>    public class MainActivity extends ActionBarActivity {
  
  Button b1;
  Button b2;
  Button b3;
  Button b4;
  Button b5;
  Button b6;
  Button b7;
  Button b8;
  Button redo;
  
  TableLayout t1;
  
  LinearLayout l1;
  LinearLayout l2;
  LinearLayout l3;
  LinearLayout l4;
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      t1= (TableLayout) findViewById(R.id.table);
  
      l1= (LinearLayout) findViewById(R.id.ll1);
      l2= (LinearLayout) findViewById(R.id.ll2);
      l3= (LinearLayout) findViewById(R.id.ll3);
      l4= (LinearLayout) findViewById(R.id.ll4);
  
      b1= (Button) findViewById(R.id.button1);
      b2= (Button) findViewById(R.id.button2);
      b3= (Button) findViewById(R.id.button3);
      b4= (Button) findViewById(R.id.button4);
      b5= (Button) findViewById(R.id.button5);
      b6= (Button) findViewById(R.id.button6);
      b7= (Button) findViewById(R.id.button7);
      b8= (Button) findViewById(R.id.button8);
  
  
      ....
  
  }
  
  
  public void shuffle(){
  
      l1.removeAllViews();
      l2.removeAllViews();
      l3.removeAllViews();
      l4.removeAllViews();
  
      ArrayList&lt;Button&gt; a=new ArrayList&lt;Button&gt;(8);
      a.add(b1);
      a.add(b2);
      a.add(b3);
      a.add(b4);
      a.add(b5);
      a.add(b6);
      a.add(b7);
      a.add(b8);
  
      l1.addView(a.remove((int)(Math.random()*8)));
      l1.addView(a.remove((int)(Math.random()*7)));
      l2.addView(a.remove((int)(Math.random()*6)));
      l2.addView(a.remove((int)(Math.random()*5)));
      l3.addView(a.remove((int)(Math.random()*4)));
      l3.addView(a.remove((int)(Math.random()*3)));
      l4.addView(a.remove((int)(Math.random()*2)));
      l4.addView(a.remove(0));
  
  
  }
  
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>}</p>