As I have already developed a news reader app for my blog, I got asked quite a few times if I want to share my code to help others creating their apps. As I need to redesign my blog reader to match the latest OS features of Windows and Windows Phone, I decided to create my WordPressUniversal library.
Automattic, the company behind WordPress, integrated a powerful JSON API into their JetPack plugin. My library is based on this API. Please make sure the JSON API is active on the blog you are developing your app for.
The library currently provides the following features:
getting a list posts or pages
getting a list of all categories
getting a list of comments for the site and single posts
supports Windows Phone 8 & 8.1 Silverlight, Windows Phone 8.1 RT, Windows 8, Xamarin.iOS and Xamarin.Android
The library deserializes all data into C# objects you can immediately work with.
It is still a work in progress, but as it provides already a bunch of options to get you started, I am making this public already.
I am constantly working on the library, so be sure to follow the project along.
Note: JetPack’s JSON API does not support guest commenting at the moment. I already reached out to Automattic to (hopefully) find a solution for this. If you cannot wait, Disqus has a portable solution for Windows Phone apps.
I love to learn and expand my knowledge. Because of this, I was absolutely happy when I was asked for a book review about Dimitris’ iOS book.
The book is a huge collection of iOS recipes using Xamarin. The first three chapters are going deeply into the UI of an iOS application, looking on a lot (almost all) possible aspects of UI elements. What I like very much is that the author shows the code, usually with a step by step guide, and after that delivers a detailed explanation why something works in the way it does.
The next two chapters are all about creating and displaying data, files and sqlite, providing the same experience as the first chapters.
The sixth chapter is all about consuming services, such as web services, REST services or even WCF services (I wasn’t even aware of this being possible). Very good starting point for so many app ideas.
So far, the book shows already a lot of what we can do with Xamarin. But modern apps often contain media content: videos, photos, capturing media – this is what chapter 7 is all about.
Like all modern Smartphone operating systems, iOS provides some methods to let our apps interact with the OS. The 8th chapter is all about those interactions, like contacts, mail and more and has the matching real world scenarios.
The most usable apps use a device’s sensors, touch and gestures. Of course, with Apple being the leader in this space for a long time (we just need to be fair in this point), iOS has a lot of APIs for these. Chapter nine has some good recipes to help us with improving our app’s UX.
If your app needs location services and maps, chapter 10 is your friend. It shows you how to interact with Apple’s map services, add annotations and a lot more.
Users love when apps have some nice animations when something changes in an app. iOS provides a lot of options, and chapter 11 explains a lot about animations and drawing methods.
One of the most important parts when developing an app is lifecycle handling. As with any other OS, also iOS has its specific methods to handle the lifecycle. Background operations are part of this handling. In chapter 12, Dimitri tells us a lot about handling the states of an app as well as background operations.
Chapter 13 consists of tips and recipes for localization of an iOS app.
One of the most important steps when creating an iOS app is deploying the app. Apps should of course be tested on real devices, and this what chapter 14 is about – but not only. Also the required steps to prepare and app for submission as well as the submission to the store are explained.
The final chapter contains some additional recipes that can make your app more valuable like content sharing or text-to-speech.
Conclusion
I only began with Xamarin.iOS a few month ago. This book provides a great insight into development for iOS using the Xamarin IDE. As I said already, I like the approach of showing code first and then explaining what it does exactly and provide additional info if suitable. This book is absolutely worth every single cent if you want to start with iOS and Xamarin.
If you are coming from C#/Silverlight, Android can be a little diffusing. Android does not have the same structure than a Windows or Windows Phone app.
Activities
Android has Activities. Activities are the key classes of Android were all actions take place. Unlike other apps or programs, you do not have a “Main” program that is your starting point when launched.
In Android, the starting point is an Activity. The Activity needs to be declared as the starting point. When you start a new project in Xamarin, an Activity called “MainActivity” gets created automatically.
The ‘Label’ attribute is what you will see in the Title bar when launching the app. The attribute ‘MainLauncher=true’ tells the application to start from here. Think of this as your MainPage.xaml.cs in a Windows Phone app.
Every Activity has its own OnCreate event, where you can put all your starting, button handlers, stylings etc. in.
Those events are important to understand for a lot of your application logic, like:
saving data that has to be persistent
starting and pausing animations
register and unregister external events
fetch data that are passed from other Activities
and more (we will cover some of them in my further posts)
I absolutely recommend to go to the link above to read and understand those events.
Context
The Context is often needed in an Android application and allows your code to be run within an Activity.
The Context allows you for example:
to access Android services,
to access Application resources like images or styles,
to create views
to assign gestures to an Activity
Without Context, often code does not get accepted by the IDE or makes your code getting ignored while running the app. Luckily, all methods that I used so far have been telling me if they want a Context, so you need only learn to find out if you need Context for the Activity resources or Application resources.
Intents
But what if we want to have another page (like a separate about page for example)? How can we navigate between pages or call external functions?
This is what Intents are for. Intents are sending messages through the application if another function needs to be started, like the launch of a second page. Within these intents, we have declare the actions that the app needs to do, and if we need a callback, this will also be passed with an Intent.
Let’s hold this high level, here are some lines of code that navigate to a second page (Activity):
var second = new Intent(this, typeof(SecondActivity)); StartActivity(second);
With this code, we are creating an new Intent with Context to our current running Activity to launch the SecondActivity.
To send data between Activities, we use the PutExtra() method of Intents:
var second = new Intent(this, typeof(SecondActivity));
second.PutExtra("FirstPage", "Data from First Page");
StartActivity(second);
Of course, we need also some code to read the passed data on our second page:
Intent.GetStringExtra("FirstPage") ?? “Data not available”;
We could now use this data on our second page by assigning it to a control or function.
Passing data between activities works similar to passing QueryStrings in NavigationsService.Navigate() method on WindowsPhone, so you should get familiar with it very fast.
Views
The last part that is very important are Views. In a View you declare how the Activity looks like. Think of it as your MainPage.xaml page in a Windows Phone app.
Views can be very different, too. Let’s start with the simple View. In our getting started project, we also have a Layout file that holds our first View:
The View has a main LinearLayout which is acts as ContentPanel. All other controls like Buttons, TextViews etc. are going into this. There are a lot of properties that can be set, we’ll leave it this easy for now. If you want to know more about those properties, you can have a look at the Android documentation here: http://developer.android.com/guide/topics/ui/declaring-layout.html
This part is totally the same as with the one matching part in the Android SDK, so you will need to get familiar with the Android documentation as well.
To make a View visible, we need to assign it to an Activity. In our sample app, we are doing this with this line of code in our OnCreate event:
SetContentView (Resource.Layout.Main);
This is the easy way for Views. But there are more complex Views in Android, too.
Views are also used in ListViews, where you declare the look of the items and the list in a Layout file, or also if you use the ActionBar with a tab navigation, and also in the menu of an ActionBar. I will cover all of these in my further blog posts, as we also need Adapters to get data binded to Views.
I hope you this post helps you to understand the high level structure of an Android application.
2013 was a year with a lot of surprises. It was a year full of community work for me as well as a huge learning year in development. But my year had also dark clouds on heaven. This post is my personal review of 2013 – you can like my impressions or not.
I started the year with releasing my first Windows 8 app ever, along with an huge update to my blog reader app for Windows Phone. I wrote several blog posts and started also development of my NFC Toolkit app for Windows Phone (Archive: January). I also ran a beta test for my NFC Toolkit, and finished my series about the parts that should help other developers to write a blog reader app for both Windows and Windows Phone (Archive February & Archive March).
Then in April, the first time I had dark clouds hanging deeply in my life, affecting all parts – family, community work and also my 9to5 job. My wife had once again problems with her back, caused by slipped discs. It went as far as she needed to rest in hospital for a pain therapy. Luckily this therapy was helping her and our life went back to normality (knocking on wood).
I also started a new series on the WinPhanDev blog – Why we started developing (WWSDEV). We are collecting stories from developers, and posting them over there to motivate other developers and keep the community spirit alive. Just have a look, we have really great stories over there.
In the last days of April/beginning of May, Iljia engaged me to start using Windows Azure Mobile Services to make an app idea reality: TweeCoMinder was born. It is a very special and unique app, interesting for those that don’t want to miss their special counts on Twitter, supported by real push notifications via WAMS for both Live Tiles and Toast Notifications. I learned a lot during setting up my WAMS for the app, and I did also write some blog posts about that (AzureDev posts).
Because of TweeCoMinder, I stopped developing my NFC app for that time, and did only bug fixing updates to my other apps.
In August (at least in the spare time I had), I moved my blog completely to run in a Windows Azure VM. I did it to get more control over the whole system and to learn more about running a web service. I still need to write my blog posts about setting the VM with LAMP on Azure, but I just didn’t have time for that until now. In August/September I also had again very very dark clouds hanging around, with my wife was very ill (you can’t even imagine how happy I am about the fact she has this part behind her). But our daily live is still affected by this – we just learned to arrange us with the new situation.
In October, I got back to my NFC Toolkit to finish it finally. The app has some cool and unique features utilizing NFC tags, and I am quite satisfied with my download numbers. NFC Toolkit is my main project for the moment.
But also on my 9to5 job I came to write code. I was asked to write an internal app for Windows Phone (Telefónica has a partnership with Microsoft, and so the company is flooded with Windows Phones). I used this to learn more about speech recognition on Windows Phone, as this is part of the application (Make your app listening to the user’s voice).
And finally, I also started with my very first Android app using Xamarin while porting the Windows Phone app I wrote before. I recently started to blog about my experiences with Xamarin (read more here).
In between all those projects, I made a basic reader app for the fan blog “This is Nokia”, using a PCL project for both Windows Phone 8 and Windows 8. I also wrote a simple car dashboard app to integrate it in my NFC Toolkit app as well as Mix, Play & Share, which was written on a lonely Saturday night while my kids where sleeping an my wife was at her best friend.
Through the year, I learned a lot of coding, but also a lot about people. I made some very positive experiences – but also bad ones. I am always willing to help (if my still growing knowledge enables me to do so) – but sharing a feature rich app to another person isn’t helping – if you want to learn about development, there are plenty ways to do so. We have really great developers that blog about their experiences in our community, and by understanding how to code, you truly learn. Just using an already working app and restyling it, is the wrong way.
Well, that is what my year was about – a lot of coding, learning and again coding.
Dear followers, friends, WinPhans & WinPhanDevs – thank you for being with me this year. Let’s make 2014 an even more exciting year.
I wish you all “a good slide into the new year”, as we say here in Germany. May god bless you and your families also in the new year.
As I mentioned already in my first blog post about Xamarin, Android has a different project structure – even if you use Xamarin.
A very important part of this structure are resources. We have different kind of resources in an Android app:
Icons & Images
Layout (XML files)
Values (string resource files)
Let’s start with Icons & Images.
As you can see in the solution window, there is a folder called ‘Resources’. Here you will put all kind of resources, like images, icons, layouts and so on.
The corresponding class for images in Android is called ‘drawable’, that holds a reference to’ Icon.png’. The project structure is based on that, that’s why the resources folder has all images inside the folder ‘drawable’. As Android has various screen sizes, you may have a folder for structure like ‘drawable’, ‘drawable-hdpi, ‘drawable-ldpi’ and so on. Android scales your image resources to match the screen automatically if you do not define alternate layouts.
To make your files available in your app, you need to set the Build Action to ‘Android Resource’:
Let’s have a look to the Layout files:
Layout files are XML files that contain the UI of a page, control or a view. You can use these XML files for example to define items of a ListView or simply the application page. Every new project has a ‘Main.axml’ file.
There are two ways to create a layout. The first one is using the visual designer:
This works for basic layouts like adding the button above. Also, if you don’t know the properties of a control you added, you will be able to add it here to get started.
If you are familiar with writing your UI in code (like XAML) and want to do so in your Android app, just click the ‘Source’ tab at the bottom in the visual designer. You will see something like this:
If you want to add and modify a control, but don’t know how the properties are, this page has a list of all controls, which are called ‘widgets’ in Android. That’s also the corresponding namespace: android.widget.
Like in an Windows Phone app, you also have a string resource file in Android projects. This file is once again a XML file, but with a different structure:
All strings need to be declared inside the <resources> tag. The definition is always like <string name=”yourstringname”>stringcontent</string>. Empty strings are not supported and will throw an error at building your project.
Let’s have a look on how we can work with our resources, based on our gettingstarted project. We have the following code inside our MainActivity class:
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Text = GetString (Resource.String.hello);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
As you can see, the we are defining our view from our Layout file ‘Main.axml’ by using the SetContentView() method. The file is added to our resources list as Layout with the name we defined.
Our MainActivity does not know that we have a button inside our layout. To make the button visible to our MainActivity, we need to reference it. This is done by declaring a Button instance and using the FindViewById<T>(ResourceName) method.
If you have not given your button a name, now is the right time to do so. In our example the button has the name “myButton”. The syntax is very important, make sure you add “@+id/” to the name.
android:id="@+id/myButton"
Now our button is visible to our MainActivity code page and can be accessed. The sample project references the button content in the Layout file:
android:text="@string/hello"
After referencing our button, we could also use the following code to get the button content from the resource file:
button.Text = GetString (Resource.String.hello);
Whenever your want to get a string from the resource file in an Activity, the GetString() method is used.
I hope this post helps you to understand how resources are used in an Android app and how to handle it a Xamarin project.
Xamarin comes with some emulators for development of an Android app. While these emulators are working and are also available in all supported screen sizes, the performance of them is really bad.
That’s why I chose debugging via device, which is a lot faster than the emulators once you have the required Xamarin extensions installed.
Here is how to set up an Android device for debugging. First thing you need to know is which OS version is running. On most Android devices, you will find this information under Settings/About (or Info)/Software information. If you don’t have the result there, you should check the manual where to find this information on your device.
Activate Debugging until Android 3.2
go to Settings from your Application Menu
choose Applications
open the Development item
you will see now the option to activate USB debugging:
Activate Debugging on Android 4.0, 4.1
go to the Settings Menu
choose Developer options and activate USB debugging:
Activate Debugging on Android 4.2 and higher
go to the Settings Menu
select About
tap the Build Number 7 times (after the 4th tap a notification will appear that counts down the required taps)
Installing drivers
Now our devices are ready to be connected to our PC, where we need to install the corresponding USB drivers. Normally you just need to plug in your Android device, Windows will search for the right driver and install it for you. If you want to install the driver manually, here is a list of OEM driver packages: http://developer.android.com/tools/extras/oem-usb.html#Drivers
Deploying an App for debugging
After the drivers have been installed, your device should now be recognized by Windows and also by Xamarin Studio.
Using our gettingstarted project from my first blog post, you should have now two options to deploy the application to your device:
Run With…
Just hit ‘Debug’…
Xamarin Studio installs now the required packages for debugging, and after that, you will be able to debug you application.
Speeding up re-deployments
Xamarin will install all developing packages including your app from scratch every time you deploy the app. To speed things up, there is an option to speed things a little up.
To activate/check if this option is already activated, click on the ‘options’ button of your project and select ‘Options’ in the context menu:
Select ‘Android Build’ and activate these two options:
This way, Xamarin will deploy only changes that have been made to your code, while keeping the required packages on your device. Xamarin Studio will detect if an update to those packages is required.
However, sometimes you will need to clean your solution and do a complete re-deploy (like you are doing with a Windows Phone project sometimes, too).
In very seldom cases (happened 3 times to me by now), you will need to uninstall your app and all of the Xamarin packages from your device. To do so, go to Settings/Apps on your device. In the application list, you will find some apps that start with ‘Mono’:
To uninstall them, tap the items and select ‘Uninstall’ in the App info screen:
Xamarin Studio will now re-deploy all required packages to your device, and your app should run again for debugging.
Like always, I hope this post is helpful for some of you.
In the last few month I was developing not only my personal projects, but also on my daily 9to5 job. I am working on an internal app that will help to improve the customer experience amongst the customers of my employer Telefónica.
The project has a simple setup in phase I, with collecting data and sending them via mail. The challenge for me was how to get the Windows Phone app (yes, Windows Phone was first!) to Android.
I knew it would take some time to get started with Android development, and was searching for possible solutions to speed things up. I knew from Xamarin already, and was able to obtain a 1-year license for Android (maybe iOS will follow later).
Some of my fellow WinPhans and WinPhanDevs will scream out very loud at the moment. I understand that. But honestly, I could not let go away this huge opportunity for me – as it touches both my daily job and my private developer story. To calm you down: I will always love Windows Phone and Windows and develop for it first. I will always remain a WinPhanDev. But I need to go forward, and this is a big step.
Let’s go back to the topic.
With this blog post, I am kicking off a new blogging series for Android development with Xamarin. The huge advantage of using Xamarin is that I am able to use my C# knowledge to develop apps for other platforms – which makes it a little easier to get things done.
However, if you start with another platform, you still have to learn the platform structure. Without knowing or be willing to learn it, you will be lost very fast. Every OS has its own specific UI rules, save handling and so on. This is what I will blog about.
The Windows Phone app I ported to Android has a item Pivot, which I ported over to Android. It has a little different appearance, but it works in a similar way with tapping the header or a swiping gesture to move between the items/pages.
To get there, I had to learn a lot about Android – and I will share it all with you out there.
My series will cover the following topics:
installing Xamarin and getting started (this post)
using SQLite to save data temporarily and permanently
using Intents to send Mail or save a Calendar entry
getting contacts from address book
Lists and Adapters
create a SplashScreen
and more… (I will update this list with links to the corresponding blog posts)
Installing Xamarin
Installing Xamarin is pretty easy – but will take some time. Click on this link to download the Xamarin IDE. The IDE will ask you for which platform you want to develop for (let’s choose only Android for the moment) and then download a huge amount of additional SDKs like the Java and the Android SDK (Android apps are Java based, if you didn’t knew).
On the screenshot above you can see the starting page of Xamarin Studio. On the left side you have the list of recent solutions, in the middle you see Xamarin news and finally the pre-built apps section.
Xamarin Studio has a lot together with Visual Studio, and you also can take a lot of settings to adjust the appearance for your needs. Let’s start a new project by clicking on the ‘New’ button
Xamarin will set up a .sln file with everything we need for the moment. After creating the project has been created, you’ll see these lines of code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace gettingstarted
{
[Activity (Label = "gettingstarted", MainLauncher = true)]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
}
}
These few lines should help to get started with the basic structure of an Android App.
Android has Activities. Activities can be seen as the code behind files in a C# project. Here begins all the action of an Android app. Xamarin automatically ads the Label (project name) and in this case, also the MainLauncher property, which will run this code on app start up.
To get something displayed in our app, we need to load a Layout or create the view from the code. This is what the SetContentView() method is about. Without this, the app will compile but displays nothing. In this demo app, a basic layout with a button inside is created:
We already have the first difference to other C# projects here. In order to make the button visible for our code in the Activity, we need to search our resources for the button. This is done by the FindViewById<T>() method. As soon as it is visible to our code, we are able to create a delegate for our button Click event.
The button counts then the clicks on it and changes the text based on the number of clicks.
To understand more about the structure of an Android App, I highly recommend to read the documentation. For me, especially these links were helpful:
There are some more resources that are helpful, but for getting started those four links should be enough. I will cover some of the topics above also with my upcoming posts and link additional resources as well.