0

I want to validate my edittext for my number that is range between 100 and 200.So according this i have created InputFilterMinMax class.When i am running my app, i can not write anything into my edittext because when i insert 1 in InputFilterMinMax class return "". I want to insert 100 into my edittext.

This is edittext xml :

<EditText
    android:id="@+id/txtTajavozArea"
    android:layout_weight="0.5"
    android:gravity="center_horizontal"
    android:inputType="numberDecimal"
    android:selectAllOnFocus="true"
    android:singleLine="true"/>

After initialized my ui i have created a method for ui validation because some validation comes from my server and i have to apply on my android ui. Then im oncreate method i have:

LoadUi();
validateForm();
LoadObj();

Into validateForm method i have this part of code :

String viewIdName = cursor.getString(cursor.getColumnIndex("ControlName"));
int id = getResources().getIdentifier(viewIdName, "id", this.getPackageName());
View view = findViewById(id);
if (view instanceof EditText) {

if (!cursor.getString(cursor.getColumnIndex("MaxValue")).equals("null") &&
        !cursor.getString(cursor.getColumnIndex("MinValue")).equals("null")) {
    editText.setFilters(new InputFilter[]{new InputFilterMinMax(
            Integer.parseInt(cursor.getString(cursor.getColumnIndex("MaxValue"))),
            Integer.parseInt(cursor.getString(cursor.getColumnIndex("MinValue"))))});
}

As you can see i get my view in this way because my view once loaded into LoadUi() method.

But as i mention above i can not write anything!!

This is InputFilterMinMax class:

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;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

1 Answers1

0

In your code snippet

if (!cursor.getString(cursor.getColumnIndex("MaxValue")).equals("null") &&
        !cursor.getString(cursor.getColumnIndex("MinValue")).equals("null")) {
    editText.setFilters(new InputFilter[]{new InputFilterMinMax(
            Integer.parseInt(cursor.getString(cursor.getColumnIndex("MaxValue"))),
            Integer.parseInt(cursor.getString(cursor.getColumnIndex("MinValue"))))});
}

You are going from MaxValue to MinValue so the MinValue and the MaxValue just needs to be swapped. It should be

if (!cursor.getString(cursor.getColumnIndex("MaxValue")).equals("null") &&
        !cursor.getString(cursor.getColumnIndex("MinValue")).equals("null")) {
    editText.setFilters(new InputFilter[]{new InputFilterMinMax(
            Integer.parseInt(cursor.getString(cursor.getColumnIndex("MinValue"))),
            Integer.parseInt(cursor.getString(cursor.getColumnIndex("MaxValue"))))});
}
IsaiahJ
  • 454
  • 7
  • 19
  • Sort is important? @Isaiahj – Cyrus the Great Sep 29 '18 at 07:57
  • Yes, order is important because your input filter accepts the parameters InputFilterMinMax(int min, int max) so MinValue should go first. – IsaiahJ Sep 29 '18 at 07:59
  • I can not insert anything into my edittext, again. because when i insert 1 , 1 is not range between 100 and 200 and 1 is not register into edittext @ IsaiahJ – Cyrus the Great Sep 29 '18 at 08:03
  • And again when i insert next number, this number is first and absolutely this number is between 100 and 200 @ IsaiahJ – Cyrus the Great Sep 29 '18 at 08:06
  • I see. If you check the comments on your [source here](https://stackoverflow.com/a/14212734/6902410), it seems that the InputFilterMinMax really doesn't work for ranges like 100 to 200. – IsaiahJ Sep 29 '18 at 08:10
  • Then this is not useful code.. it seems like the number must be start from 1 :-( @IsaiahJ – Cyrus the Great Sep 29 '18 at 08:14
  • Yes unfortunately. I'd suggest using a Spinner instead or just validating the EditText upon form submission. – IsaiahJ Sep 29 '18 at 08:18