Two TextWatchers creating an infinite loop

Viewed 537

I have 3 EditText fields in my android app that I'm trying to have communicate with each other. They are order total, amount received, and tip. The user enters the price of the order in the first EditText. The other two EditTexts are the ones I'm wanting to modify based on the value of each one. If the user enters 20.00 in the order total and 25.00 in the amount received, I would like the tip to display 5.00. Likewise, if they enter 20.00 in the order total, and 5.00 in the tip, I would like amount received to display 25.00. I have all the calculations correct, and each one of my TextWatchers work as long as the other one is commented out, but with both of them it just creates an infinite loop. Is there a good way to do this, or can someone please point me in the right direction to some helpful links? I can't seem to find any that work for what I'm trying to do.

My code is as follows:

eTotalCost = (EditText) findViewById(R.id.orderTotal);
eAmountReceived = (EditText) findViewById(R.id.amountReceived);
eTip = (EditText) findViewById(R.id.tip);
eMileage = (EditText) findViewById(R.id.mileage);
eGrandTotal = (EditText) findViewById(R.id.grandTotal);

eAmountReceived.addTextChangedListener(new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after){}

    @Override
    public void onTextChanged(CharSequence s, int start,
                                  int before, int count){}

    @Override
    public void afterTextChanged(Editable s)
    {
        eAmountReceived = (EditText) findViewById(R.id.amountReceived);
        eTotalCost = (EditText) findViewById(R.id.orderTotal);
        eTip = (EditText) findViewById(R.id.tip);
        eMileage = (EditText) findViewById(R.id.mileage);
        eGrandTotal = (EditText) findViewById(R.id.grandTotal);

        sAmountReceived = eAmountReceived.getText().toString();
        sTotalCost = eTotalCost.getText().toString();
        sMileage = eMileage.getText().toString();
        sTip = eTip.getText().toString();

        try
        {
            dAmountReceived = Double.parseDouble(sAmountReceived);
            dTotalCost = Double.parseDouble(sTotalCost);

            dSubtract = dAmountReceived - dTotalCost;
            dMileage = Double.parseDouble(sMileage);
            dGrandTotal = dSubtract + dMileage;
        } catch(NumberFormatException e){}

        sTip = String.valueOf(dSubtract);

        DecimalFormat df = new DecimalFormat("0.00");
        sTip = df.format(dSubtract);

        eTip.setText(sTip);

        sGrandTotal = String.valueOf(dGrandTotal);
        sGrandTotal = df.format(dGrandTotal);
        eGrandTotal.setText(sGrandTotal);
    }
});


eTip.addTextChangedListener(new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after){}

    @Override
    public void onTextChanged(CharSequence s, int start,
                                  int before, int count){}

    @Override
    public void afterTextChanged(Editable s) {
        eAmountReceived = (EditText) findViewById(R.id.amountReceived);
        eTotalCost = (EditText) findViewById(R.id.orderTotal);
        eTip = (EditText) findViewById(R.id.tip);
        eMileage = (EditText) findViewById(R.id.mileage);
        eGrandTotal = (EditText) findViewById(R.id.grandTotal);

        sAmountReceived = eAmountReceived.getText().toString();
        sTotalCost = eTotalCost.getText().toString();
        sMileage = eMileage.getText().toString();
        sTip = eTip.getText().toString();

        try 
        {
            dTip = Double.parseDouble(sTip);
            dTotalCost = Double.parseDouble(sTotalCost);

            dAdd = dTotalCost + dTip;
            dMileage = Double.parseDouble(sMileage);
            dGrandTotal = dTip + dMileage;
        } catch (NumberFormatException e) {}

        sAmountReceived = String.valueOf(dAdd);

        DecimalFormat df = new DecimalFormat("0.00");
        sAmountReceived = df.format(dAdd);

        eAmountReceived.setText(sAmountReceived);

        sGrandTotal = String.valueOf(dGrandTotal);
        sGrandTotal = df.format(dGrandTotal);
        eGrandTotal.setText(sGrandTotal);
    }
});
3 Answers
Related