0

In my android project I am using three text boxes. They are txtPurchase , txtSales, and txtProfit. When I enter the value 5000 in txtpurchase and 6000 in txtsal I want to then display the value of 1000 in txtprofit without a button click. Can anyone suggest how to do this?

mattfred
  • 2,689
  • 1
  • 22
  • 38
Rit
  • 1
  • 2
  • you can use onTextchanged method of editText.see the reference http://stackoverflow.com/questions/7432083/how-to-use-edittext-ontextchanged-event-when-i-press-the-number – Ankur1994a May 20 '16 at 17:27

1 Answers1

2

You can do this with an OnTextChanged listener.

txtSales.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) {
            if (s.length() > 0 && txtPurchase.getText().toString().length() > 0) {
                // logic here to do the math
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
mattfred
  • 2,689
  • 1
  • 22
  • 38