Created
December 17, 2015 09:09
-
-
Save vaibhav-jani/69170e649d8febe3f564 to your computer and use it in GitHub Desktop.
Android Edittext Filter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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