Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Baby Steps on Android with Xamarin–Part 2

Blogs

Mike Taulty's Blog

Elsewhere

Archives

Following up on my previous post the next steps seemed to be get the Xamarin bits installed on my machine.

There are a number of Xamarin editions (https://store.xamarin.com/) with the ones that sounded more attractive to me being the ones that offered Visual Studio support. Those come in at $999/$1899 per platform, per developer per year so it’s clearly got to be something that you or your company sees value in before you’re going to invest in that license fee.

Luckily for me, I was able to get an edition that I can use for demos and so on without having to cough up my own $ so I moved to the next step which is downloading and installing bits.

I’m primarily interested in Android in the first instance but I figured I may as well tick the iOS box as part of the installation;

image

then I got a nice 2GB of Android SDKs (about the same size as the Windows Phone 8 SDK if I remember rightly);

image

and then various bits of SDKs (JDKs) downloaded and installed. This takes quite a chunk of time but you do get a dialog letting you know which bit is going on;

image

Whoever would have thought that you’d need all these bits from all these organisations/companies just to build an app for a phone? Smile

I left this installing while I went off for a few hours so I couldn’t say exactly how long it took but it finished up neatly and successfully and my next run of Visual Studio 2013 took a little longer to launch than usual while Visual Studio sorted itself out but then I noticed that I have a number of new project templates inside of Visual Studio;

image

image

and I figured what the heck I’d go make an Android application just to see how that worked out and I got stuck for a little while;

image

until I managed to get some temporary credentials and then that got me to my blank project;

image

which felt pretty familiar to me both from the point of view of building an app for Windows/Phone and from the point of view of having made one or two things inside of Eclipse for Android previously.

I had a quick play with the designer and was pleased to see Document Outline support and some level of Properties window support;

image

where the properties window seemed to have some support for an idea of resources in the sense that clicking on an entry would raise this dialog;

image

and I played around with the Toolbox a little, randomly adding controls onto the layout on the screen;

image

I also spotted the various different device layouts I could work with;

image

I was curious as to how these resources worked and so I went and read the AboutResources.txt file included with the project which I thank whoever included because it was really useful. It taught me a little around how the app can deal with resources at different DPI (this is perhaps trying to achieve similar ends to the WinRT way of doing things).

Based on that I had a look at the supplied String.xml resource file;

image

and felt obliged to add a string to it, hit compile and see if a suitably generated property turned up on the Resource class. Sure enough, A led to B;

image

I was a bit curious about how the Activity1 class generated by the template related to the Main.axml UI that I was playing with on the designer and I resolved that really by looking at the code in Activity1.cs;

image

and while I don’t have that quite figured out, I can at least see how the pieces fit together (including that FindViewById<Button> call on the next line) and the manual syncing of the event handler.

I presume (just a guess at this point) that the MainLauncher property on that Activity attribute also perhaps sets this as the “main” activity for the app.

In looking at the project’s references;

image

I felt obliged to wander into the object browser on some of these and I was pleased to see (alongside LINQ 2 XML which I really like) things like XmlReader, XmlWriter, XmlDocument in there and I was relieved to see that System.Threading.Tasks was in there. I was also a bit surprised to spot that .NET Remoting seemed to be in there too.

I had a glance into AssemblyInfo.cs where I wasn’t really expecting to find surprises but I did spot;

image

so I guess that’s one way (at least) of specifying what permissions my app needs. I had a look at Android.Manifest.Permission which was interesting in itself having first thought it was an enum but it turned out to be a class full of constant strings set up presumably to match names of Android permissions that I think I remember putting into a manifest/config file when working with the ADK directly in Eclipse;

image

Speaking of manifests, I took a quick peek into AndroidManifest.xml and was surprised to see that I was targeting SDK version 8 as I expected somehow to be on 18 or something like that;

image

I found a properties page on the solution itself;

image

which seemed to provide a graphical way of editing at some/all of the values in that manifest XML file. I thought the next page;

image

might also edit my AssemblyInfo.cs file to add more attributes to define the permissions required but it didn’t seem to be doing that so perhaps I need to figure that out a bit more.

The last thing I thought I’d try in poking around here was just to see if I could get the code up and running in a debugger. I set a breakpoint in the generated activity code;

image

Then wasn’t exactly sure what I was meant to be doing to get the debugger up and running. There’s a dropdown;

image

and I wasn’t sure whether this related to debugging or something else (it’s on its own toolbar). I tried the button next but one on the right to it in order to open up the Android Emulator Manager which gave me a dialog that I remember from the ADK (or somewhere like that);

image

and I wondered whether I should be defining a new device here to match my random selection of API level 18. I imagined that it might be worth a try so I tried it and then realised that the tool didn’t seem to let me go beyond API level 15 so I tried to run what I’d got on that level 15 device. At that point, it turned out that I didn’t have the right .jar file installed for level 18 so I reverted my project back to level 15 and tried again.

The tooling even told me what I already knew about the emulators;

image

but I went along with it. I don’t want to appear partisan but compared to the Windows Phone emulators, the Android ones have always seemed to be “not very good” to me but after a short eternity (about 10 minutes) the OS booted.

I think my next step might be to get better emulators – these are shockingly poor (I like the Windows Phone ones on my touch screen as I can then drive the emulator via touch).

I waited a long time and nothing happened other than the OS booted up. Visual Studio thought it was in the middle of a build but it did let me cancel that “Build” and so I tried again, once again waiting for the lethargic (Google) emulator to spring into life.

I had a few attempts at this and didn’t quite get it to work. I decided that I might have broken the project in some way so I made another blank project and tried to run that.

Once again though I got Visual Studio stuck in a “Build” phase.

Finally, I just grew a lot more patience and, after monitoring the Output window in Visual Studio I realised that things weren’t broken. They were just so slow as to appear broken.

And, in the end the app showed up on the Android device in the emulator.

By that point though I’d got to reading the Xamarin page about getting hold of Intel emulators (link is here http://docs.xamarin.com/guides/android/deployment,_testing,_and_metrics/configuring_the_x86_emulator/ ) and that will be my next step when I get some bandwidth to do the download because, as they stand, the emulators feel unusable to me even on a decently spec’d laptop.


Posted Tue, Feb 25 2014 9:13 PM by mtaulty

Comments

Thomas LEBRUN wrote re: Baby Steps on Android with Xamarin–Part 2
on Tue, Feb 25 2014 10:02 PM

Hi Mike,

Great serie of posts on Xamarin!

You could also take a look to one of my latest article, dedicated to Xamarin and Android: blog.thomaslebrun.net/.../introduction-to-androidios-development-using-visual-studio-and-xamarin

Thomas

James Montemagno wrote re: Baby Steps on Android with Xamarin–Part 2
on Wed, Feb 26 2014 12:23 AM

Mike,

ChrisNTR sent me your way. First off awesome series! I can't wait to see where this goes.

A few things is to make sure that you lower case your Values, Layout, and Drawable folders under resources. This is a bug in the templates, everything will show up properly if you do this. :)

Next up for image loading take a look at my image loading helper on github which will really simplify that process for you in Android:

github.com/.../MonoDroidToolkit

Finally for x86 emulators make sure to install the proper x86 drive. I have a post about it: motzcod.es/.../lets-fix-that-x86-android-emulator

Cheers and feel free to reach out to me on Twitter: @JamesMontemagno

Richard wrote re: Baby Steps on Android with Xamarin–Part 2
on Wed, Feb 26 2014 5:09 PM

Its also worth checking out the Genymotion emulators as they seem to be much quicker than the stock emulator

http://www.genymotion.com/

Simone basso wrote re: Baby Steps on Android with Xamarin–Part 2
on Thu, Feb 27 2014 8:39 AM

Try Genymotion for a faster Android Emulator

Kiliman wrote re: Baby Steps on Android with Xamarin–Part 2
on Fri, Feb 28 2014 2:34 AM

Unfortunately even with Genymotion emulators, you need to install Intel HAXM to get hardware acceleration. The problem is that it is not compatible with Hyper-V, so you can't have WP8 emulators running at the same time. You need to disable Hyper-V and reboot.

I use an actual Android device (Nexus 4) and use Mobizen to mirror the phone to the PC. This has worked really well.

Mike Taulty's Blog wrote Baby Steps on Android with Xamarin–Part 4
on Mon, Mar 3 2014 4:27 PM

Following up on previous posts; Baby Steps with Xamarin on Android - Part 1 Baby Steps on Android with