O
O
Orkhan Hasanli2018-08-11 23:18:39
Android
Orkhan Hasanli, 2018-08-11 23:18:39

How to limit number range from 10 to 50 for editext?

Hello!
I wondered about limiting the range of input numbers for the EditText component.
And found a solution:

public class InputFilterMinMax implements InputFilter {

    private int min, max;

    public InputFilterMinMax(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterMinMax(String min, String max) {
        this.min = Integer.parseInt(min);
        this.max = Integer.parseInt(max);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) { }
        return "";
    }

    private boolean isInRange(int a, int b, int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

ageField.setFilters(new InputFilter[]{new InputFilterMinMax("10","50")});

<EditText
            android:id="@+id/age_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:layout_gravity="center"
            android:layout_margin="10dp"/>

However, after building the project, nothing can be entered in the field at all. What is my mistake?
Source - blog.adeel.io/2016/09/11/inputfilter-used-on-andro...
This option only works if the number (minimum) is 1, and if the minimum number is set to 10 or any other number, then it does not works...
Thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-08-12
@azerphoenix

Decision -

public class InputFilterMinMax implements InputFilter {

    private int min, max;

    public InputFilterMinMax(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterMinMax(String min, String max) {
        this.min = Integer.parseInt(min);
        this.max = Integer.parseInt(max);
    }



    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            String prefix = dest.toString().substring(0, dstart);
            String insert = source.toString();
            String suffix = dest.toString().substring(dend);
            String input_string = prefix + insert + suffix;
            int input = Integer.parseInt(input_string);

            if (isInRange(min, max, input) || input_string.length() < String.valueOf(min).length())
                return null;
        } catch (NumberFormatException nfe) { }
        return "";
    }

    private boolean isInRange(int a, int b, int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question