Reputation: 368
I am building an android application where user input 2 value in two editText That take only Integer value's of length 2.
The user Enter maximum value and minimum value in this EditText.
Now The problem Is is there any way so that I can restrict the user to fill any greatest value In minimum Edittext then Maximum Editext.
May be on Submitted button or my other source.
Here is code -
max = (EditText) dialogView.findViewById(R.id.max);
min = (EditText) dialogView.findViewById(R.id.min);
Please help....
Upvotes: 0
Views: 865
Reputation: 2260
You should create an InputFilter for EditText. Visit this link
First create a class implementing InputFilter:
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;
}
}
Then set Min and Max values to bound each edittext:
EditText et = (EditText) findViewById(R.id.your_edittext);
et.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "100");
Note that you can use other EditText's value to set other Editbox's MaxValue (If you need)
Regards
Hana Bizhani
Upvotes: 2
Reputation: 2779
on your submit button handler you can check if the value of min is greater than the value of max and show the user a suitable message
int maxVal = Integer.valueOf(max.getText().toString().trim());
int minVal = Integer.valueOf(min.getText().toString().trim());
if (minVal > maxVal){
...
}
Upvotes: 1