Xamarin: Lollipop Support comes to the Xamarin Android Player

Waiting for an emulator to boot or deploy is one of the most frustrating things about Android development. We built the Xamarin Android Player to provide a high-performance, hassle-free Android simulator, and to make Android development smooth and pleasant. Introducing Lollipop Support Starting today, when you launch the Xamarin Android Player you will see a […]

The post Lollipop Support comes to the Xamarin Android Player appeared first on Xamarin Blog.

Xamarin: More Material Design for Your Android Apps

Android developers have been flocking to Material Design since its introduction with the release of Android Lollipop. With the recent update to the Support v7 AppCompat library it has never been easier to add Material Design to target older Android operating systems. Material Design is much more than just the core theming and styling that […]

The post More Material Design for Your Android Apps appeared first on Xamarin Blog.

Jérémie Laval: Advanced transitions with vector drawable

As I described in a previous serie of post, vector drawables (and their animated counterparts) are one of the most powerful new visual elements introduced with Lollipop (and soon as a support lib it seems).

In this post, I’m gonna show you how we can achieve the following transition animation by solely using vector drawables and Android’s built-in widget state tracking mechanism:

Staging

To build an animation like this, you have to prepare the adequate stage. Afterwards, it’s just a matter of moving each individual pieces of that stage when needed.

In the above video, we can split the final visual in a couple of different parts.

Some like the center dot and the upper-half donut are static pieces, they never move. The two white thin rectangles, symbolizing the hands on a watch, are individual moving parts. The sun, moon and exclamation mark symbols are the varying state of the visual depending on the input.

Now, that’s only the visible part of the puzzle. If you look closely at the animation you can notice that everytime there is a state transition the right hand seems to be erasing the visible time symbol before the new one fade in.

This is where things get interesting because to be able to achieve this effect you have to get creative with the Z-ordering of your stage.

To understand this better, below is a rendering of all the layers of the visual:

The rectangle you see covering the half-bottom part of the picture is not an artifact. It’s painted with the same color than the overall window background thus acting as a cheap but effective hiding mask.

If I conceal this rectangle, here is what’s lurking behind: the other half of the visible top donut

To create the aformentioned effect, we will be moving that half-bottom donut to cover the top one, synchronizing it with the right clock hand. When not in use, it will be safely tucked behind our hiding rectangle.

Because vector drawables have a simple bottom to top drawing hierarchy, for this to work correctly we have declare pieces in a specific order:

  • top half donut
  • the 3 time symbols
  • bottom half donut
  • hiding rectangle
  • the 2 hands white rectangle

Based on our animation needs, we then split this further into actual vector drawable <group /> elements to arrive at the following shape (path data removed for brevity, you can download all the XML pack at the end of the article):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
	android:height="385dp"
	android:width="416dp"
	android:viewportHeight="385"
	android:viewportWidth="416">
	<group android:name="background">
		<path
			android:name="topPie"
			android:fillColor="#454D58"
			android:pathData="..." />
	</group>
	<group android:name="timeItems">
		<path
			android:name="moon"
			android:fillColor="#DAE2ED"
			android:fillAlpha="0"
			android:pathData="..." />
		<path
			android:name="sun"
			android:fillColor="#E39300"
			android:fillAlpha="0"
			android:pathData="..." />
		<path
			android:name="now"
			android:fillColor="#C52E26"
			android:fillAlpha="1"
			android:pathData="..." />
	</group>
	<group
		android:name="hidingPie"
		android:pivotX="208"
		android:pivotY="192.5">
		<path
			android:name="hidingPiePath"
			android:fillColor="#454D58"
			android:pathData="..." />
	</group>
	<group android:name="coverBackground">
		<path
			android:name="blueCover"
			android:fillColor="#343A42"
			android:pathData="..." />
		<path
			android:name="middleRing"
			android:fillColor="#D8D8D8"
			android:pathData="..." />
	</group>
	<group
		android:name="leftHand"
		android:pivotX="208"
		android:pivotY="192.5">
		<path
			android:fillColor="#D8D8D8"
			android:pathData="..." />
	</group>
	<group
		android:name="rightHand"
		android:pivotX="208"
		android:pivotY="192.5">
		<path
			android:fillColor="#D8D8D8"
			android:pathData="..." />
	</group>
</vector>

State tracking widget

Before we dwelve into some more animation tricks, we first need to investigate how to make widget to represent our triple state because there is nothing in the default framework that allow this.

The good news is that, as I showed in a previous blog, it’s very easy to create a custom, state-aware widget which is what we are going to do with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class TriStateView : View
{
	int selectedIndex;

	public int SelectedIndex {
		get {
			return selectedIndex;
		}
		set {
			if (value < 0 || value > 2)
				throw new ArgumentOutOfRangeException ("value");
			JumpDrawablesToCurrentState ();
			selectedIndex = value;
			RefreshDrawableState ();
			Invalidate ();
		}
	}

	protected override int[] OnCreateDrawableState (int extraSpace)
	{
		switch (selectedIndex) {
		case 0:
			return new int[0];
		case 1:
			return new int[] { Android.Resource.Attribute.StateFirst };
		case 2:
			return new int[] { Android.Resource.Attribute.StateLast };
		default:
			return base.OnCreateDrawableState (extraSpace);
		}
	}
}

By setting our state transition drawable as the background of this view, it will automatically react to state changes and animate accordingly. Using the normal state tracking mechanism also means that interruption mid-animations are gracefully handled for us.

One more trick

At this stage, I won’t go over every details of the individual animation definitions, they all tweak standard things like alpha and rotation on each piece of our stage. Nonetheless, you can read my animated vector drawable entry for more details on how they work.

I will only show a little trick that is useful when setting up more complex stages like this: zero-duration animators.

Take the following example that animates the bottom-half donut (to create the progressive hiding effect):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:ordering="sequentially">
	<objectAnimator
		android:duration="850"
		android:propertyName="rotation"
		android:valueFrom="0"
		android:valueTo="180"
		android:valueType="floatType"
		android:interpolator="@android:interpolator/decelerate_quad" />
	<objectAnimator
		android:duration="0"
		android:propertyName="rotation"
		android:valueFrom="-180"
		android:valueTo="0"
		android:valueType="floatType" />
</set>

The idea is that right after we have covered the entire upper-half, we want to immediately tuck it back under our hiding rectangle. This is because we then fade-in the new time symbol which would be hidden too otherwise.

Using zero-duration animators allow you to do just that by applying direct values to animated properties. The fact that there is a from attribute doesn’t matter much but since those properties are write only it will prevent object animator from complaining about missing accessors.

I use this in a couple of places to reset the stage state (since vector drawable instances are reused across transitions), quickly move away elements or hide them from view when they are not useful anymore.

Source

You can download the XML definitions and widget source here.

Bug alert: there is currently a parsing bug where the animated vector drawable inflater will greedily read the entire document given to it. This causes an issue if you declare them inline in your animated state transition XML file because it will corrupt your entire state by reading everything instead of stopping at its corresponding end tag. The workaround is to always declare them in their own XML files like I did.

Daniel Hindrikes: Xamarin.Forms Android CardView

When Google introduced Material Design for Android they introduced a new view called CardView. Xamarin.Forms doesn’t have support for CardView by default but you can easily create your own view that renderers a CardView on Android. First step is to create a Xamarin.Forms control in your shared project. public class CardContentView : ContentView {   […]

James Montemagno: Material Design Theming for Xamarin.Forms Android Apps

I am an extremely big fan of Material Design for Android applications. Case and point is this tweet that I sent out last night:

Over the past six months have I present on Material Design at Xamarin Evolve and also at user groups and the number one question I get is: “How can we use Material Design in Xamarin.Forms?” 

That is a loaded question because Material Design is not only the core theming of the application, but it is custom controls, animations, transitions, and a plethora of other things. Check out Google’s Material Design guidelines  for everything that is really part of material design. 

When it comes to Xamarin.Forms and Material Design it is a tricky question. All the custom control, animations, transitions, and jazz like that you most likely will not be able to do with out some work and some renderers.  However, you still have the ability to add in a little Material Design Theming and of course you can follow proper spacing and guidelines on the 4dp grid.

The AppCompat Debate

Revision 21 of AppCompat v7 was pretty ground breaking. Traditionally, AppCompat brought the use of the ActionBar to older platforms, but Revision 21 changed all of this by bringing Material Design Themes and controls to older devices. This is important since Lollipop is currently 3.5% of the market! Currently, Xamarin.Forms does not use AppCompat which means we can not take advantage of the new compat theming, but there is NO reason that we can’t make our Lollipop users happy by adding a little material theming to their app. This will bring not only material design themes, but also all the fancy touch events, and other core Lollipop control features from the core OS, which will be very nice.

Base Styles:

The first step is to ensure that you have you Resources/values/styles.xml and base theme setup to use Holo. In your Android project under Resources/values simply create a new xml file and name it styles.xml. Then you can place this XML in it:

Next is to create our Lollipop specific styles under Resources/values-v21/styles.xml, which you will place:

I like to put all of my color resources in Resources/values/colors.xml, and this is what mine looks like, which you can see I am referencing in the values-v21/styles.xml:

Update ApplicationManifest.xml

You must now of course tell your Android app to use this new fancy theme that you have created by setting the android:theme attribute:

Remove App Icon from ActionBar

The new default in Lollipop is to hide the app icon from the main action bar and you can easily do this inside of your MainActivity.cs with just a few lines of code:

Update Hamburger Button (optional)

If you are using the MasterDetailPage you will want to update your hamburger icon so that on v21 it uses the new full triple line. You can do this by download the set from the Android Asset Studio and then creating new drawable folders with the extension of -v21 at the end like this:

Then you will set the Icon to “slideout.png” or whatever you called it for your MasterDetailPage.

Here is the final result of just a few minutes of work in my Hansleman.Forms app:

Nice new styling for the colors, larger action bar, better fonts, and of course a nice new hamburger button! So can you get Material Design in your Xamarin.Forms application? Absolutely, but you just don’t get everything and it is for a limited amount of users. However, the number of Lollipop users is only going up so why not add a bit of flare to your app?