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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Improving ui-select Control
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • In-Depth Guide to Using useMemo() Hook in React
  • Iptables Basic Commands for Novice

Trending

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • Automatic Code Transformation With OpenRewrite

Xamarin.Android: Implementing An Input Filter

By 
Ryan Alford user avatar
Ryan Alford
·
Mar. 20, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
10.4K 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.

Related

  • Improving ui-select Control
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • In-Depth Guide to Using useMemo() Hook in React
  • Iptables Basic Commands for Novice

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: