Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Xamarin.Android: Implementing An Input Filter

DZone's Guide to

Xamarin.Android: Implementing An Input Filter

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

In a recent project, I needed to limit the input of the user in an EditText control. For my purposes, I needed to limit the user to a minimum and maximum number that could be allowed. To do that, I needed to implement the IInputFilter interface.

So first, I created my class called MinMaxInputFilter, inherited from Java.Lang.Object, and created a constructor to accept the minimum and maximum values.

public class MinMaxInputFilter : Java.Lang.Object
{
     private int _min = 0;
     private int _max = 0;
 
     public MinMaxInputFilter (int min, int max)
     {
          _min = min;
          _max = max;
     }
}

The reason for inheriting from Java.Lang.Object is because we are implementing an interface. This is something that is required by Xamarin.Android. For more information, see this post.

Next, we need to implement the IInputFilter interface. We update our class declaration.

public class MinMaxInputFilter : Java.Lang.Object, IInputFilter
{
     private int _min = 0;
     private int _max = 0;
 
     public MinMaxInputFilter (int min, int max)
     {
          _min = min;
          _max = max;
     }
}

Then we implement the FilterFormatted method…

using System;
using System.Diagnostics;
using Android.Text;
 
public class MinMaxInputFilter : Java.Lang.Object, IInputFilter
{
     private int _min = 0;
     private int _max = 0;
 
     public MinMaxInputFilter (int min, int max)
     {
          _min = min;
          _max = max;
     }
 
     public Java.Lang.ICharSequence FilterFormatted (Java.Lang.ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend)
     {
          try
          {
               string val = dest.ToString().Insert(dstart, source.ToString());
               int input = int.Parse(val);
               if (IsInRange(_min, _max, input))
                    return null;
          }
          catch (Exception ex)
          {
               Debug.WriteLine ("FilterFormatted Error: " + ex.Message);
          }
 
          return new Java.Lang.String (string.Empty);
     }
 
     private bool IsInRange(int min, int max, int input)
     {
          return max > min ? input >= min && input <= max : input >= max && input <= min;
     }
}

In this method, we receive the existing text as the dest parameter, and the text that is being added as the source parameter. The dStart and dEnd parameters tell us where the character is trying to be added to existing text. For this, I insert the character that is trying to be added into the existing text and check to see if that value is in my range.

For implementation, we will use the SetFilters method on the EditText control.

var myEditText = FindViewById<EditText>(Resource.Id.MyEditText);
myEditText.SetFilters (new Android.Text.IInputFilter[]{ new MinMaxInputFilter

And lastly, we need to make sure that our EditText has it’s inputType set to number.

android:inputType="numberSigned"

And that’s it. The EditText control will now limit the user to a number between 0 and 1000, and will handle if the user moves the cursor around in the EditText.


Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Published at DZone with permission of Ryan Alford, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}