Android Open Source - sdl_tester_android Min Max Input Filter

From Project

Back to project page sdl_tester_android.


The source code is released under:

Copyright (c) 2014, Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project sdl_tester_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.livio.sdl.viewhelpers;
/* www. j ava  2  s  . com*/
import android.text.InputFilter;
import android.text.Spanned;

 * An input filter for number-based text inputs.  The filter allows the user
 * to enter any values between the min and max values used in the constructor,
 * but will not allow any letters or numbers outside that range to be entered into
 * the text field.  For best results, set inputType to number for any text inputs
 * using this input filter.
 * @author Mike Burke
public class MinMaxInputFilter implements InputFilter {

  private int min, max;
  public MinMaxInputFilter(int min, int max){
    this.min = min;
    this.max = max;
  public MinMaxInputFilter(String min, String max){
    this.min = Integer.parseInt(min);
    this.max = Integer.parseInt(max);
  public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
      int input = Integer.parseInt(dest.toString() + source.toString());
      if(isInRange(min, max, input)){
        return null;
    }catch(NumberFormatException e){
      // do nothing
    return "";
  // determines if the input is in range or not
  private static boolean isInRange(int min, int max, int input){
    if(max > min){
      return (input >= min && input <= max);
      return (input >= max && input <= min);


Java Source Code List