Xamarin.Android – Implementing An Input Filter

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 (0, 1000) });

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.


2 Comments

  1. Garry April 14, 2014 7:26 pm  Reply

    I think its broken!
    Tried with min=3 and max=60.
    It would not let me enter 10.

  2. admin April 22, 2014 11:38 am  Reply

    @Garry

    Can you post a gist of your code?

Leave a comment

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