Professional Documents
Culture Documents
MENU
Android iOS
1. Jump Right In
Welcome to Xamarin.Android! Once the installation is complete,
follow these simple instructions to write and test your first Android
C# code in minutes! You can also visit our Developer Center.
Download Tasky
http://xamarin.com/getting-started/android 1/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
The solution and project structure in the Solution pane shows all the files in the
solution and should look familiar to any Modern IDE user. It consists of two projects
containing a number of C# files.
If you cannot see the solution tree, choose View → Solution from the menu.
Check that the Debug configuration is selected in the toolbar and choose Run →
Debug from the menu or press Command + Return to start debugging with the
Android Emulator.
Before an emulator can start you need to choose an Android Virtual Device (AVD) to
use, as shown in these screens:
http://xamarin.com/getting-started/android 2/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
Once the emulator has started you can add some tasks to see how the application
works - touch the Add Task button to begin!
Before looking at the code more closely, let’s revisit the solution and application
architecture. There are two projects, which help facilitate a cross-platform solution
when combined with Xamarin.Android:
The architecture and project structure of the Tasky application is shown in the
following diagram:
The application is separated into Layers (each represented by a namespace), and each
layer has a specific purpose:
Data Access Layer - Abstraction layer between the business logic and the data
layer.
Data Layer - Low-level data persistence and retrieval; the Tasky sample uses a
SQLite database binding.
2. Modifying Tasky
As it sits, Tasky lacks a basic feature of any decent task application:
the ability to mark a task complete. We’re going to add that
http://xamarin.com/getting-started/android 4/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
We’re also going to spruce up the design of the application a bit by modifying the
navigation bar style. After we've made all of these changes, here is how Tasky will look:
Open the Task.cs file in Tasky.Core.Android and notice the class has the following
property:
Tasky uses ADO.NET to store and retrieve data using the built-in SQLite database
engine. All data operations can be performed with familiar SQL syntax and ADO.NET
classes. Here's an example of how ADO Connection and Command objects can be
http://xamarin.com/getting-started/android 5/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
Now that we’ve modified our Task object, let’s modify the user interface to allow
users to mark them as complete.
Screens in Xamarin.Android are typically defined by an XML layout file (with the
.axml suffix) and one or more C# subclasses of Android.App.Activity that load
that layout. We need to modify both the AXML and the C# to add the Done property to
our user interface.
<CheckBox
android:id="@+id/chkDone"
android:layout_below="@+id/NotesText"
android:layout_width="wrap_content"
http://xamarin.com/getting-started/android 6/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
android:layout_height="wrap_content"
android:text="Done" />
<Button
android:id="@+id/SaveButton"
android:text="Save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/chkDone"
/>
<Button
android:id="@+id/CancelDeleteButton"
android:text="Cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/SaveButton"
/>
The Task Details screen will now render a checkbox under the Notes input.
The following changes are required to hook up the checkbox to the business object:
CheckBox doneCheckbox;
Find the checkbox control and set its value in the OnCreate method:
doneCheckbox = FindViewById<CheckBox>(Resource.Id.chkDone);
doneCheckbox.Checked = task.Done;
task.Done = doneCheckbox.Checked;
http://xamarin.com/getting-started/android 7/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
The updated task business object is then saved to the database via the Business
Layer’s TaskManager class. This completes the changes to the Task Details screen - it
will now display and save the new property we added to our business object.
The final step is to display the completion status of each task on the home screen.
Each row in the task list is rendered via calls to the GetView method of
TaskListAdapter.cs file in the Adapters folder. We need to alter the code to use a
CheckedTextView and then set the Checked property. To do this replace the custom
TaskListItem layout with the SimpleListItemCheck layout:
As we have changed the layout, delete the code describing the old Task List and replace
it with code that matches the CheckedTextView properties
The application now looks like this – the task completion status is visible in the list and
can be marked as Done on the Task Details screen using the checkbox control:
http://xamarin.com/getting-started/android 8/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
So now Tasky is a bit more useful, but let’s spice it up a bit by modifying the style of
the application bar. Let’s change its color to a nice blue.
http://xamarin.com/getting-started/android 9/11
25/8/2014 Start building mobile apps for Android in C# in under 10 minutes - Xamarin
The Android.Views namespace will need to be included in this file, you can easily add it
by right-clicking on the code and resolve using Android.Views
There are lots more styling changes you can make to an Android application, including
hiding the application bar altogether and replacing it with your own custom view. Now
the user interface looks like this:
Awesome, we’ve now played with our first application in Xamarin.Android. We’ve seen
Xamarin Studio in action, and we built and tested an application in the emulator.
3. Next Steps
Great job! You're on your way to becoming an awesome mobile
developer.
Now, we recommend going through our Android Getting Started Tutorial Series
Open tutorial
http://xamarin.com/getting-started/android 11/11