Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jani
Created December 17, 2015 09:09
Show Gist options
  • Select an option

  • Save vaibhav-jani/69170e649d8febe3f564 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhav-jani/69170e649d8febe3f564 to your computer and use it in GitHub Desktop.
Android Edittext Filter.
import android.text.InputFilter;
import android.text.Spanned;
/**
* Created by vaibhav.jani on 17-Dec-15.
*/
public class FilterProvider {
public static InputFilter getFilter(final int maxDigitsBeforeDecimalPoint, final int maxDigitsAfterDecimalPoint) {
InputFilter filter = new InputFilter() {
//final int maxDigitsBeforeDecimalPoint = 4;
//final int maxDigitsAfterDecimalPoint = 1;
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([1-9]{1})([0-9]{0," + (maxDigitsBeforeDecimalPoint - 1) + "})?)?(\\.[0-9]{0," + maxDigitsAfterDecimalPoint + "})?"
)) {
if (source.length() == 0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
};
return filter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment