Custom Dialog with Xamarin Android

Custom Android Dialog

This is a continuation of my last post where I showed how to save and retrieve an item to the Android SQLite database using SQLite.Net ORM. In this post I want to show how to use Custom Dialog with Xamarin Android to add an item to the same database. Tutorials on Android Master List details normally show either two Activities or an Activity and a Fragment. In this post, I will use the Main Activity already added in the last post and then create a Custom Dialog with Android. Below is what the completed dialog will look like and the code for this tutorial can be found in Github.
Custom Dialog Android
Just to re-state what I will be implementing in this tutorial, there will be a button on the Main Activity called “Add Services” that when clicked will launch a custom dialog over the Activity. A little interesting twist is that there is a drop down list for the Categories. This could have been a text field, but in real life Categories are often drop down list that are backed by a data source. In Android, drop down list are implemented with the Spinner class. So I have not only added a Spinner but I have added the ability to populate that Spinner on the fly so the user can add a Category that does not yet exist and have that Category become immediately available on the drop down list. The data source for the drop down list is yet another table in the SQLite database. Let’s start with the UI for the Custom dialog.

User Interface for a Custom Dialog with Xamarin Android

If you are following from the last tutorial with this source code, go ahead and add a layout file under layout sub-folder of the Resources folder. I have named my file AddServiceDialog.xaml and here is what the design tab will look like. You can design iwth the designer or you can just go ahead and copy and paste the XML code below.

What we have here is a Linear layout with two nested Linear Layout wrapped with a ScrollView. Notice the Checkbox, what that does is it hides and shows a field that is used to add a Category. For example if a user is adding a Service called Income Tax Preparation Service and the Category for this service is called “Professional Service” if this category already exist in the drop down list then the user can just select it there is no need to show the field to add category in this case but if the Category does not exist then the user can click the Checkbox which will expand the fields to show the Add Category field.

Category Table
Since we are saving the categories to the database, we need to create a Table to hold the list of categories. And since we are using the SQLite.Net to manage persistence we need to to create a class that the ORM will work with. So go ahead and add a class to the Model folder called ServiceCategory.cs and the this class only has a Name property for now and it inherits from the IBusinessEntity this way we can re-use the existing database logic which works with anything that extends IBusinessEntity.

And then you add a statement to create the Category table to the ServiceDatabase.cs file like so

We will also need to create CategoryRepository class that will handle the CRUD operation for the Categories class and also create a ServiceManager.cs that uses Repository. This may appear like unneccessary overhead but it does allow for code maintainability in the long run. I believe that it is possible to have a Generic Repository of type T so that we do not have to create a new repository class each time. I will look into that later. Look at the Github repo for the repository and manager class code.

Creating a Custom Xamarin Android Catalog
Add a file to the root of the project called ServiceDialog.cs, this is a standard C# class and we will go ahead and make it inherit from DialogFragment. In this class we will do the following:

  • Create a method that instantiates and returns a new instance of this dialog
  • Inflate the view for this dialog which is the layout we created above
  • Create and initialize the properties for this Dialog
  • Add event handler for the drop down list (Spinner)
  • Add event handler for the Save button
  • Add listener that tells the calling Activity that an item has been added through the dialog

And here is the code for the Custom dialog with Xamarin Android

Notice how we are not showing the dialog after creating it but returning it to the calling Activity. Now from the calling Activity, the MainActivity.cs in this case we just need to add a standard menu item to the Menu with text set to show always like so:

With this we can now instantiate the custom dialog when someone clicks on the “Add Services” button in the menu overflow like so:

And that is it, with this you are now able to add an item to the Custom list and you will be able to add categories dynamically. And with a little massaging you should be able to re-use the same dialog to edit the item. Because of the way that the custom listview was created, the new items will show up immediately after you add them because after you dismiss the custom dialog box and return back to the MainActivity, the onResume method is called and in there is a method called LoadData() which reloads the data for the custom listview including the item that you just added so the listview is up to date. That is it with adding custom dialog with Xamarin Android. Happy coding

About the Author valokafor

I am a Software Engineer with expertise in Android Development. I am available for Android development projects.

follow me on:

Leave a Comment: