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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat

Xamarin.Android: Implementing An Input Filter

Ryan Alford user avatar by
Ryan Alford
·
Mar. 20, 14 · Interview
Like (0)
Save
Tweet
Share
9.71K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Unlocking the Power of Polymorphism in JavaScript: A Deep Dive
  • Apache Kafka vs. Memphis.dev
  • Debugging Threads and Asynchronous Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: