Monotouch Core Location Example Essay

Dialog is like any other window that pops up in front of current window, used to show some short message, taking user input or to ask user decisions. Unlike , a dialog is generally used where user attention is mandate. Android supports several different ways to create a dialog such as and . In this example we will cover all the aspect of AlertDialog and DialogFragment.

1. Using AlertDialog in Xamarin.Android

is the subclass of that can display one, two or three buttons. If you only want to display a String in this dialog box, use the method.

The following code snippet can be used to create a simple AlertDialog with two buttons Delete and Cancel.

The above code snippet will produce the output as shown in the following screenshot.

2. Using DialogFragment in Xamarin.Android

Since the release of Android 3.0 (API level 11), fragment can show as a dialog and called as . If you’re supporting older android versions, you can make use of fragment-compatibility support library.

To create a dialog fragment, we will be using DialogFragment class. This class is derived from the Fragment and behaves much like a fragment with all available fragment life cycle methods. Android recemends to use DialogFragment over AlerDialog.

You need to perfrom the following steps to create a DialogFragment

  • Create a new class that extends from DialogFragment class.
  • Like regular Fragments, override  callback to attach the dialog layout.
  • Alternatively, you can override  method and return a Dialog instance. This method is used to port your old AlertDialog code without much of modification.

In this example, we will see both OnCreateView() and OnCreateDialog() callback.

3. Dialog fragment using OnCreateDialog()

The following code snipept shows how to create dialog by overriding OnCreateDialog() method.

4. Dialog fragment using OnCreateView()

DialogFragment is like any other fragment, the same lifecycle rules are applied. Now we have to override onCreateView method to attach the layout to view hierarchy and construct the dialog fragment.

Let us first define the layout for your fragment. In this example, I have used two TextViews and Button. My layout looks as follows:

Now let us inflate the layout from OnCreateView() method. My DialogFragment class looks as follows:

The above code snippet will produce the output as shown in the following screenshot.

5. Adding DialogFragment

We are pretty much done!. Add the following code snippet in your Activity to instantiate and display the dialog;

The above code snippet will produce the output as shown in the following screenshot.

The Core Location namespace revolves around tracking and monitoring user location, the device's entry-and-exit from regions (either fixed or iBeacon-based), and geocoding between addresses and locations.

Geocoding use-cases center on the CLGeocoder class. Geocoding is simple:

C# Example

async void GeocodeToConsoleAsync (string address) { var geoCoder = new CLGeocoder(); var placemarks = await geoCoder.GeocodeAddressAsync(address); foreach (var placemark in placemarks) { Console.WriteLine(placemark); }

As is reverse-geocoding:

C# Example

async void ReverseGeocodeToConsoleAsync (CLLocation location) { var geoCoder = new CLGeocoder(); var placemarks = await geoCoder.ReverseGeocodeLocationAsync(location); foreach (var placemark in placemarks) { Console.WriteLine(placemark); } }

Other use-cases typically will start with the CLLocationManager class:

  • Tracking the device's location
  • Region monitoring (Geofencing)
  • iBeacon ranging

These use-cases are discussed in the CLLocationManager class documentation.


One thought on “Monotouch Core Location Example Essay

Leave a Reply

Your email address will not be published. Required fields are marked *