Question

Could you guys lend me some fresh eyes and tell me what where I should look to find the missing link? I am trying to do a mortgage calculation by pulling value from purchasePrice, downPayment, and interestRate EditText fields to do the calculation. I tested the updateStandand() by setting the TextEdit field to interest rate. However, when I run the app in AVD, if I type a purchase price, the monthly payment field would reflect the price. If i move down to interest rate field and type in a number, the monthly payment field would change to the value of the interest rate.

Thank you!

package com.example.mortgagecalc;

import com.example.mortgagecalc.R.string;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.text.Editable;
public class Main extends Activity {

    private static final String PURCHASE_AMOUNT = "PURCHASE_AMOUNT";
    private static final String CUSTOM_LENGTH = "CUSTOM_LENGTH";
    private static final String DOWN_PAYMENT = "DOWN_PAYMENT";
    private static final String INTEREST_RATE = "INTEREST_RATE";

    private int currentPurchaseAmount; //purchase amount entered by user
    private int currentCustomLength; //length of loan set with SeekBar
    private int currentDownPayment; //down payment entered by user
    private double currentInterestRate; //interest rate entered by user
    private EditText purchaseAmountEditText; //accepts user input for purchase amount
    private EditText downPaymentEditText; //accepts user input for down payment amount
    private EditText interestRateEditText; //accepts user input for interest rate
    private EditText tenYearEditText; //display monthly payment for 10yr. loan
    private EditText twentyYearEditText; //display monthly payment for 20yr. loan
    private EditText thirtyYearEditText; //display monthly payment for 30yr. loan
    private EditText customMonthlyEditText; //display monthly payment for custom length
    private TextView customLengthTextView; //display custom loan length

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //check if app just started or being restored from memory
        if (savedInstanceState == null) //app just started running
        {
            currentPurchaseAmount = 0; //initialize to zero
            currentCustomLength = 10; //initialize custom loan length to 10
            currentDownPayment = 0; //initialize to zero
            currentInterestRate = 0.0; //initialize to zero
        }
        else
        {
            currentPurchaseAmount = savedInstanceState.getInt(PURCHASE_AMOUNT);
            currentCustomLength = savedInstanceState.getInt(CUSTOM_LENGTH);
            currentDownPayment = savedInstanceState.getInt(DOWN_PAYMENT);
            currentInterestRate = savedInstanceState.getDouble(INTEREST_RATE);
        }

        purchaseAmountEditText = (EditText) findViewById(R.id.purchaseAmountEditText);
        downPaymentEditText = (EditText) findViewById(R.id.downPaymentEditText);
        interestRateEditText = (EditText) findViewById(R.id.interestRateEditText);
        tenYearEditText = (EditText) findViewById(R.id.tenYearEditText);
        twentyYearEditText = (EditText) findViewById(R.id.twentyYearEditText);
        thirtyYearEditText = (EditText) findViewById(R.id.thirtyYearEditText);
        customMonthlyEditText = (EditText) findViewById(R.id.customMonthlyEditText);
        customLengthTextView = (TextView) findViewById(R.id.customMonthlyTextView);

        purchaseAmountEditText.addTextChangedListener(textWatcher);
        downPaymentEditText.addTextChangedListener(textWatcher);
        interestRateEditText.addTextChangedListener(textWatcher);
        SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar);
        customSeekBar.setOnSeekBarChangeListener(customSeekBarListener);
    }

    private void updateStandard(){

        double interestRate = currentInterestRate;
        //double loanPrinciple = currentPurchaseAmount - currentDownPayment;

        //double tenYearMonthlyPayment = loanPrinciple;
        tenYearEditText.setText(String.valueOf(interestRate));

    }

    private void updateCustom(){

        //customLengthTextView.setText(currentCustomLength + "year");
        double customInterestRate = ((currentInterestRate/12) * .01);
        int customLoanPrinciple = currentPurchaseAmount - currentDownPayment;
        double customMonthlyPayment = customLoanPrinciple * customInterestRate / 
                                      (1-Math.pow(1+customInterestRate, -(currentCustomLength*12)));
        customMonthlyEditText.setText(Double.toString(customMonthlyPayment));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);

        outState.putDouble(PURCHASE_AMOUNT, currentPurchaseAmount);
        outState.putDouble(DOWN_PAYMENT, currentDownPayment);
        outState.putDouble(INTEREST_RATE, currentInterestRate);
        outState.putDouble(CUSTOM_LENGTH, currentCustomLength);
    }

    private OnSeekBarChangeListener customSeekBarListener = new OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            currentCustomLength = seekBar.getProgress();
            updateCustom();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar)
        {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar)
        {
        }
    };

    private TextWatcher textWatcher = new TextWatcher()
    {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            try
            {
                currentPurchaseAmount = Integer.parseInt(s.toString());
                currentDownPayment = Integer.parseInt(s.toString());
                currentInterestRate = Integer.parseInt(s.toString());
            }
            catch(NumberFormatException e)
            {
                currentPurchaseAmount = 0;
                currentDownPayment = 0;
                currentInterestRate = 0.0;
            }

            updateStandard();
            updateCustom();
        }

        @Override
        public void afterTextChanged(Editable s)
        {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Was it helpful?

Solution

You're using a single TextWatcher for all three fields, and updating all three fields in the onTextChanged method.

I'm guessing that what you really want is three separate TextWatcher instances - one for each field, and to update only that one value in each one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top