I’ve been meaning to try my hand at making a mobile app for quite awhile, and I had a free Saturday afternoon and decided to finally give it a go.
I’m developing on Win 10 with Visual Studio 2015 Community edition. I have the Xamarin and Android tools installed. One thing to note is that Hyper-V needs to be enabled in order for the Android phone emulator to work. This was a minor issue for me as I use VMware Workstation as my vmplatform of choice. I needed to install Hyper-V and will be unable to run VMware will the feature is enabled. More of a nuisance that I’ll need to reboot everytime I need to switch gears.
Choosing an Idea
For a first app, I didn’t want to dive in and attempt a huge complicated project or even use too many native features, I wanted to kick the tires on Xamarin and get a little feel for how development works within it. I’ve been using the Pomadoro technique for awhile to ensure my focus on critical tasks. I haven’t really found an app version that I’m in love with right now so might as well make it the way I’d like it. For the uninitiated, the Pomadoro technique is basically 25 minutes of uninterrupted work followed by a short 5 minute break with a longer 15-20 minute break as the 4th break. Ideally you’d like to be able to work uninterrupted during your Pomadoro and be able to handle your distractions during your breaks.
Creating your Project
I created a new App, and Selected the Single-View App template under Android. This will create the structure and basic resources that we need in order get our project working.
Assuming you have your Android SDK and Hyper-V running, debugging the default solution will open up a simple app that increments as a button is clicked. Neat. The solution is comprised of XAML layouts what will comprised our view, XML resources, and Csharp code files. I’m not going to dive into the MVVM pattern with my initial go, but look for that update in a future post. I also removed a few of the default files – the GettingStarted.Xamarin, AboutAssets.txt, and AboutResourses.txt files can get binned.
Getting Started With Views
I doubled clicked the Main.axml file to open up the visual designer. The views are XAML based so if you’ve ever worked with WPF in the past it’s basically the same idea. I added a TextView and a Button in a Linear Layout. It’s a simple matter of editing a few properties to get the app looking as you’d like.
Wiring up the Timer
Android comes with a convenient Chronometer control that would handle timers perfectly. Unfortunately there is one major problem for this use case – the Chronometer only counts up from 0 – there aren’t any built-in controls to handle a countdown timer. No problem as we can make use of the System.Timers.Timer class to help us out. Notice in the snippet below that the MainActivity class is decorated with Activity – this is necessary to indicate that this is an entry point for the application and encompasses the code that supports the view. With the OnCreate event, the Timer is getting initialized with an EventHandler added to it, the Main View is attached to the code, and the other UI elements are initialized.