1 /*
2  * Copyright 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.animationsdemo;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.support.v4.app.NavUtils;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 
30 /**
31  * This sample demonstrates how to use system-provided, automatic layout transitions. Layout
32  * transitions are animations that occur when views are added to, removed from, or changed within
33  * a {@link ViewGroup}.
34  *
35  * <p>In this sample, the user can add rows to and remove rows from a vertical
36  * {@link android.widget.LinearLayout}.</p>
37  */
38 public class LayoutChangesActivity extends Activity {
39     /**
40      * The container view which has layout change animations turned on. In this sample, this view
41      * is a {@link android.widget.LinearLayout}.
42      */
43     private ViewGroup mContainerView;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         setContentView(R.layout.activity_layout_changes);
49 
50         mContainerView = (ViewGroup) findViewById(R.id.container);
51     }
52 
53     @Override
onCreateOptionsMenu(Menu menu)54     public boolean onCreateOptionsMenu(Menu menu) {
55         super.onCreateOptionsMenu(menu);
56         getMenuInflater().inflate(R.menu.activity_layout_changes, menu);
57         return true;
58     }
59 
60     @Override
onOptionsItemSelected(MenuItem item)61     public boolean onOptionsItemSelected(MenuItem item) {
62         switch (item.getItemId()) {
63             case android.R.id.home:
64                 // Navigate "up" the demo structure to the launchpad activity.
65                 // See http://developer.android.com/design/patterns/navigation.html for more.
66                 NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
67                 return true;
68 
69             case R.id.action_add_item:
70                 // Hide the "empty" view since there is now at least one item in the list.
71                 findViewById(android.R.id.empty).setVisibility(View.GONE);
72                 addItem();
73                 return true;
74         }
75 
76         return super.onOptionsItemSelected(item);
77     }
78 
addItem()79     private void addItem() {
80         // Instantiate a new "row" view.
81         final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
82                 R.layout.list_item_example, mContainerView, false);
83 
84         // Set the text in the new row to a random country.
85         ((TextView) newView.findViewById(android.R.id.text1)).setText(
86                 COUNTRIES[(int) (Math.random() * COUNTRIES.length)]);
87 
88         // Set a click listener for the "X" button in the row that will remove the row.
89         newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
90             @Override
91             public void onClick(View view) {
92                 // Remove the row from its parent (the container view).
93                 // Because mContainerView has android:animateLayoutChanges set to true,
94                 // this removal is automatically animated.
95                 mContainerView.removeView(newView);
96 
97                 // If there are no rows remaining, show the empty view.
98                 if (mContainerView.getChildCount() == 0) {
99                     findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
100                 }
101             }
102         });
103 
104         // Because mContainerView has android:animateLayoutChanges set to true,
105         // adding this view is automatically animated.
106         mContainerView.addView(newView, 0);
107     }
108 
109     /**
110      * A static list of country names.
111      */
112     private static final String[] COUNTRIES = new String[]{
113             "Belgium", "France", "Italy", "Germany", "Spain",
114             "Austria", "Russia", "Poland", "Croatia", "Greece",
115             "Ukraine",
116     };
117 }
118