{"id":3424,"date":"2013-01-14T11:22:18","date_gmt":"2013-01-14T19:22:18","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=3424"},"modified":"2013-01-14T11:22:18","modified_gmt":"2013-01-14T19:22:18","slug":"android-tricks-eye-candy-using-layout-animations","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-tricks-eye-candy-using-layout-animations\/","title":{"rendered":"Android Tricks: Eye Candy Using Layout Animations"},"content":{"rendered":"<p>\t\t\t\tSince Android 3.0 (API level 11), a lot of effort has gone into improving the visual appeal of the operating system. In addition to adding eye candy to the builtin software, Google also introduced new features in the Android development frameworks that make it easier for third-party developers to improve the look and feel of their own applications. One such improvement is the ability to add animations to existing <code>ViewGroup<\/code> elements (i.e. every layout object) without having to write the code by hand.<\/p>\n<p>When using the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/ViewGroup.html#attr_android:animateLayoutChanges\">android:animateLayoutChanges<\/a> attribute with a <code>ViewGroup<\/code>, a set of default animations are registered on the <code>ViewGroup<\/code>&#8216;s child views during layout changes. By default, visibility and size modifications are taken into account. The first produces a fading effect and the second produces a sliding animation.<\/p>\n<p>These two default effects can be used together with a <code>LinearLayout<\/code> to make a nice transition between two states of the same widget:<\/p>\n<p>In the video above, my widget has two different presentation modes: one for user selection and the other for drag-and-drop interaction. The latter mode is triggered by a user action (starting the drag).<\/p>\n<p>Here is the relevant part of my layout XML for the widget skeleton:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;LinearLayout\n    android:orientation=&quot;vertical&quot;\n    android:layout_width=&quot;fill_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;\n    android:layout_marginLeft=&quot;16dp&quot;\n    android:layout_marginTop=&quot;16dp&quot;\n    android:layout_marginRight=&quot;16dp&quot;\n    android:animateLayoutChanges=&quot;true&quot;\n    android:id=&quot;@+id\/animatedLayout&quot;&gt;\n    &lt;LinearLayout\n        android:orientation=&quot;vertical&quot;\n        android:layout_width=&quot;fill_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginBottom=&quot;7dp&quot;\n        android:id=&quot;@+id\/mode1&quot;&gt;\n\n\t\t&lt;!-- Widget mode 1 layout --&gt;\n\n    &lt;\/LinearLayout&gt;\n    &lt;LinearLayout\n        android:orientation=&quot;horizontal&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;1px&quot;\n        android:layout_gravity=&quot;center&quot;\n        android:id=&quot;@+id\/mode2&quot;&gt;\n\n        &lt;!-- Widget mode 2 layout --&gt;\n\n    &lt;\/LinearLayout&gt;\n&lt;\/LinearLayout&gt;\n<\/pre>\n<p>The outer <code>LinearLayout<\/code> is the one that contains the <code>android:animateLayoutChanges<\/code> attribute. Both widget modes are also <code>LinearLayout<\/code> objects.<\/p>\n<p>Since the first mode is the one shown by default, its layout height is set to <code>wrap_content<\/code> whereas the second mode layout is artificially hidden by setting its height to 1 pixel.<\/p>\n<p>To go from mode 1 to mode 2, I use the following code in the drag started event handler:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar parms = mode2.LayoutParameters as LinearLayout.LayoutParams;\nparms = new LinearLayout.LayoutParams (parms) {\n\tHeight = ViewGroup.LayoutParams.WrapContent,\n\tGravity = GravityFlags.Center\n};\nmode2.LayoutParameters = parms;\nmode1.Visibility = ViewStates.Gone;\n<\/pre>\n<p>And to go the other way, we simply restore the XML state we had beforehand with a similar code:<\/p>\n<pre class=\"lang:csharp decode:true\">\nmode1.Visibility = ViewStates.Visible;\nvar parms = mode2.LayoutParameters as LinearLayout.LayoutParams;\nparms = new LinearLayout.LayoutParams (parms) {\n\tHeight = 1,\n\tGravity = GravityFlags.Center\n};\nmode2.LayoutParameters = parms;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Since Android 3.0 (API level 11), a lot of effort has gone into improving the visual appeal of the operating system. In addition to adding eye candy to the builtin software, Google also introduced new features in the Android development frameworks that make it easier for third-party developers to improve the look and feel of [&hellip;]<\/p>\n","protected":false},"author":1925,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-3424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Since Android 3.0 (API level 11), a lot of effort has gone into improving the visual appeal of the operating system. In addition to adding eye candy to the builtin software, Google also introduced new features in the Android development frameworks that make it easier for third-party developers to improve the look and feel of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1925"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=3424"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3424\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=3424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=3424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=3424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}