There are 3 EditText, 1st Price input,2nd Percentage, 3rd Result. I am trying to calculate EditText inputs using onFocusChangeListener. The Code:
etPercen.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
OnFocusChangedPercenCalculator();
}
}
});
etresult.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
OnFocusChangedResultCalculator();
}
}
});
and
public void OnFocusChangedPercenCalculator() {
String text1 = String.valueOf(etPrice.getNumericValue()).toString();
String text2 = etPercen.getText().toString();
String text3 = String.valueOf(etresult.getNumericValue()).toString();
double input1 = 0;
double input2 = 0;
int i = 0;
if (text1.length() > 0 || text2.length() > 0 || text3.length() > 0)
input1 = Double.parseDouble(text1);
input2 = Double.parseDouble(text2);
if (text1.length() != 0 ){
if(text2.length() != 0) {
double output = (input1 * input2) / 100;
e = String.valueOf(output);
etresult.setText("" + e);
} else if (text2.length() ==0 || text2.matches("")){
etresult.setText(i+"");
}
}else if(text1.length() == 0){
if(text2.length() != 0) {
etresult.setText(i+"");
} else if (text2.length() ==0 || text2.matches("")){
etresult.setText(i+"");
}
}
}
public void OnFocusChangedResultCalculator() {
String text1 = String.valueOf(etPrice.getNumericValue()).toString();
String text2 = etPercen.getText().toString();
String text3 = String.valueOf(etresult.getNumericValue()).toString();
double input1 = 0;
double input3 = 0;
int i = 0;
if (text1.length() > 0 || text2.length() > 0 || text3.length() > 0)
input1 = Double.parseDouble(text1);
input3 = Double.parseDouble(text3);
if (text1.length() != 0 ){
if(text3.length() != 0) {
double output = (input3 / input1) * 100;
e = String.valueOf(output);
etPercen.setText("" + e);
} else if (text3.length() ==0 || text3.matches("")){
etPercen.setText(i);
}
}else if(text1.length() == 0){
if(text3.length() != 0) {
etPercen.setText(i);
} else if (text3.length() ==0 || text3.matches("")){
etPercen.setText(i);
}
}else if (input3 > input1){
etPercen.setText(100);
}
}
What i want to do is 2nd and 3rd EditTexts looking each other. I mean when the user change focus from 2nd/3rd editText then the calculation will begin in an instant. The idea is to make the 2nd one calculate and put the result on the 3rd and make the 3rd one calculate and put the result on 2nd. It depends on the user if he/she wants to change fill/change the value (lets say the price is 10000, the user want to know whats the 20% of it, or the user want the other way around, whats the % of 20000 from 10000). Thats my expectation, but when we emptied the 1st and 3rd edittext the output of the 2nd one will be NaN. How to handle this? i mean avoid the result become NaN?