Book Image

Xamarin Mobile Application Development for Android, Second Edition

Book Image

Xamarin Mobile Application Development for Android, Second Edition

Overview of this book

Table of Contents (18 chapters)
Xamarin Mobile Application Development for Android Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding validation


Any nontrivial app will have some level of the validation required. The POIApp app is somewhat trivial, but we have a small set of rules we need to enforce that will facilitate the discussion:

Property

Rule

Name

This cannot be empty or null

Latitude

This contains a valid decimal number between -90 and 90

Longitude

This contains a valid decimal number between -180 and 180

Using the EditText.Error property

The EditText widget has a string property named Error, which simplifies the effort of displaying errors to the user, particularly if you want to be able to show all the fields with errors at once. The following screenshot displays the error received for leaving the Name field empty:

To use this facility, simply set the property to an error message and clear the property when no errors exist. The following example demonstrates implementing the rule for the Name property:

   bool errors = false;
   if (String.IsNullOrEmpty (_nameEditText.Text)) {
   _nameEditText...