--- author: "Jim Bennett" categories: ["xamarin", "xamarin.android", "mvvmlight", "binding", "Technology", "UI", "tutorial"] date: 2016-02-18T08:59:14Z description: "" draft: false images: - /blogs/building-a-xamarin-android-app-part-5/banner.png featured_image: banner.png slug: "building-a-xamarin-android-app-part-5" tags: ["xamarin", "xamarin.android", "mvvmlight", "binding", "Technology", "UI", "tutorial"] title: "Building a Xamarin Android app - part 5" images: - /blogs/building-a-xamarin-android-app-part-5/banner.png featured_image: banner.png --- This is the fifth part in the my series about building an Android app using Xamarin.Android. I highly recommend reading these first. The previous parts are: * [Creating the basic app](/blogs/building-an-android-app-part-1/) * [Defining our data](/blogs/building-an-android-app-part-2/) * [Building view models](/blogs/building-a-xamarin-android-app-part-3/) * [Binding the view models to the UI](/blogs/building-a-xamarin-android-app-part-4/) #### Adding the Add button Currently we have a nice recycler view showing our dummy counters in card views. So the next step is to allow new counters to be added. The current way to add new items is using a floating action button - a button at the bottom of the screen that when pressed will add the new counter.
![Floating add button](floating-add-button.png)

First thing to do is add it to the layout in `counters_fragment.axml`. At the moment this contains a `LinearLayout` but we'll need to change that to a `FrameLayout` so that the add button is correctly placed on top. We're not using a normal button here, but a `FloatingActionButton`, a button designed to float above other controls in the view. This means we can add a load of counters and scroll the recycler view up and down without the add button moving from it's floating location in the bottom right. ``` ``` The icon being used here, `ic_add_white_24dp`, also comes from Googles material icons at https://design.google.com/icons/. Its a 24dp icon, the recommended size for floating action buttons. We don't need to set the colour of the action button, this automatically comes from the colours we added from Material Palette in [part 1](/blogs/building-an-android-app-part-1/). #### Adding navigation Now we have the add button, we need to think about what it needs to do. We need this button to navigate to another activity where the user can enter details about the new counter and save it. MVVMLight has a nice navigation system but we are limited in how we can use it because we are using AppCompat. Luckily I've already created a workaround which is documented [on another blog post here](/blogs/mvvmlight-navigation-and-appcompatactivity/). To add this working navigation we just need to add [the MVVMLight.AppCompat nuget package](https://www.nuget.org/packages/JimBobBennett.MvvmLight.AppCompat/), and set it up. We start with the set up by changing our `BaseActivity` to derive from `AppCompatActivityBase`. ``` public abstract class BaseActivity : AppCompatActivityBase ``` Then we need to register the navigation and dialog services with the IoC container. The concrete implementations of the services are platform specific, so we can't simply register them in the view model locator. Instead we have to expose methods to allow the registration to happen externally so that we can call it from our Android project. The following methods to register need to be added to the `ViewModelLoator`: ``` public static void RegisterNavigationService(INavigationService navigationService) { SimpleIoc.Default.Register(() => navigationService); } public static void RegisterDialogService(IDialogService dialogService) { SimpleIoc.Default.Register(() => dialogService); } ``` These are then called from our `MainActivity` in a new constructor: ``` public MainActivity() { var navigationService = new AppCompatNavigationService(); ViewModelLocator.RegisterNavigationService(navigationService); ViewModelLocator.RegisterDialogService(new AppCompatDialogService()); } ``` Whilst we're editing the `MainActivity` it makes sense to delete the dummy counter code as well - seeing as we are adding the ability to add new counters we don't need to pre-populate the app with fake counters. Just delete the `AddDummyData` method and the call to it. #### Creating the new counter To create the new counter we need to define a new activity to allow the user to enter details about the counter. This activity needs a layout, a view model and to be wired into the navigation so that we can navigate to it from our new add button. ###### Layout Lets start with the layout. This needs to have text boxes so the user can enter the name and description for the counter, along with a button to create the new counter. We're not going to bother with a cancel option - the UI will require the create button to be clicked to create the counter with back acting as a cancel, either the back button on the tool bar or the hardware back button. ```