DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • In-Depth Guide to Using useMemo() Hook in React
  • Iptables Basic Commands for Novice
  • Testing the Untestable and Other Anti-Patterns

Trending

  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Xamarin.Android: Implementing An Input Filter

By 
Ryan Alford
·
Mar. 20, 14 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.


Filter (software) Interface (computing) POST (HTTP) IT Implementation

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

Opinions expressed by DZone contributors are their own.

Related

  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • In-Depth Guide to Using useMemo() Hook in React
  • Iptables Basic Commands for Novice
  • Testing the Untestable and Other Anti-Patterns

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook