There is an awkward scenario happening in my application.
My application has functionality like EditText
for the price, where you tap on it will select all text, then you can enter a price. When user clicking on EditText
, it is selecting all text but, when the user enters any text, the first number will not be displayed. Then another number will be displayed properly. I have added TextChangeListner
and try to figure out but I am getting 0 CharSequence
while users enter the first character.
I have inputfilter for EditText
. Check below code for editfilter.
public static class InputFilterForDoubleMinMax implements InputFilter {
private double min, max;
public InputFilterForDoubleMinMax(double min, double max) {
this.min = min;
this.max = max;
}
public InputFilterForDoubleMinMax(String min, String max) {
this.min = Double.parseDouble(min);
this.max = Double.parseDouble(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
String temp = dest.toString() + source.toString();
if (source.toString().equalsIgnoreCase(".")) {
return "";
} else if (temp.toString().indexOf(",") != -1) {
temp = temp.toString().substring(temp.toString().indexOf(",") + 1);
if (temp.length() > 2) {
return "";
}
}
double input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", ""));
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) {
}
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
Any help appreciated.
The issue is resolved. the issue was due to logic implemented inside input filter.
Following is my updated code input filter.
public static class InputFilterForDoubleMinMax implements InputFilter {
private double min, max;
public InputFilterForDoubleMinMax(double min, double max) {
this.min = min;
this.max = max;
}
public InputFilterForDoubleMinMax(String min, String max) {
this.min = Double.parseDouble(min);
this.max = Double.parseDouble(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
String temp;
if (dstart == 0) {
temp = source.toString();
} else {
temp = dest.toString() + source.toString();
}
if (source.toString().equalsIgnoreCase(".")) {
return "";
} else if (temp.toString().indexOf(",") != -1) {
temp = temp.toString().substring(temp.toString().indexOf(",") + 1);
if (temp.length() > 2) {
return "";
}
}
double input;
if (dstart == 0) {
input = Double.parseDouble(source.toString().replace(",", ".").replace("€", ""));
} else {
input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", ""));
}
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) {
}
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}