I have 6 Edit Text that if the user inputs all of them will do some calculations, I want to ignore Empty Edit Text so that my app will not crashes if there is no inputs, I find way but it's painful and time consuming is to put each possibility's for edit text. For Addition I find simple why just check if the Edit Text is Empty put value 0,
Edittext.SetText("0");
So I can perform Addition if only for example 3 inputs given like ed1=1, ed=1, ed3=1
The edTotal = 1 + 1 + 1 + 0 + 0 + 0 = 3;
my problems is with other operations if division or multiply we can't set it to zero it will change the result.
And sorry still learning to code.Thanks for any Help.
I wrote some utilities methods you can use. You will need to remove default value of "0" that you are currently using. This way in case EditText
has no Integer
value calculation for that EditText
will be skipped.
Modify those functions for your needs :)
private void addValueFromEditText(EditText yourEditText) {
try {
edTotal += Integer.valueOf(yourEditText.getText().toString());
} catch (NumberFormatException exception) {
Toast.makeText(context, yourEditText + " has non Integer value", Toast.LENGTH_SHORT).show();
}
}
private void multiplyByValueFromEditText(EditText yourEditText) {
try {
edTotal *= Integer.valueOf(yourEditText.getText().toString());
} catch (NumberFormatException exception) {
Toast.makeText(context, yourEditText + " has non Integer value", Toast.LENGTH_SHORT).show();
}
}
private void divideByValueFromEditText(EditText yourEditText) {
try {
edTotal /= Integer.valueOf(yourEditText.getText().toString());
} catch (NumberFormatException exception) {
Toast.makeText(context, yourEditText + " has non Integer value", Toast.LENGTH_SHORT).show();
}
private void sumAllEditTexts() {
addValueFromEditText(ed1);
addValueFromEditText(ed2);
addValueFromEditText(ed3);
addValueFromEditText(ed4);
addValueFromEditText(ed5);
addValueFromEditText(ed6);
}
}
Edit
Added example method on how to use.
Or you can simply iterate through your container which contains those EditTextView
's