Xamarin Forms Tips Part 2 : Numeric Entry Recipes

Hi everyone, here is the second part of my Xamarin Tips series. Just as a reminder. The posts I’ll make for this series will have as case study a simple app. It will be about how I used features available in Xamarin Forms to produce unique UI for the app.

The app’s name is : My Expenditures. It is available on Play store and Microsoft store. It provides you a smart way to manage you expenses and income. And very detailed stats about how you spend or make money. You can download and use it. You will also find in the app all the features I talk about here. Let’s not waste time and dive into the numeric entry recipes.

Xamarin Forms Numeric Entry Recipes

What we will be doing here is adding a feature to an entry which will make it take only numbers. And we will also add a feature to precise the range of numbers which should be input in our numeric entry.

Numbers only

The first step will be to make our entry accept only numbers. We will implement this with Behaviors, though it could also be done using triggers. Lets create a class with the name PlainNumericEntryBehavior

First, set your entry to use a numeric keyboard. This is essential first before adding additional code.

In the method to handle this event, if the new text entered is null or empty, we wet the entry’s value to 0. Else, we check if the new value is a number. If it is, we do nothing. If it is not, we make the entry’s text to be = old text.

Limiting the value of the numbers entered

In case you have an entry in which you want users to enter numbers which are not above a specific value. For example, entering percentage or age. To perform this, we will leverage the first behavior we created above.

Add a property to the first behavior. This property will be an Action.

If you liked this post, or it was useful to you, please 👍 like it, share it on twitter, facebook or other social media… in case you want to get updated on any new useful post, follow me on twitter, Github and like my page on facebook. And every other social media by clicking on the buttons at the end of this post.