0

I am developing an app with 100's of textfields and editfields. In a lot of them I have used textwatcher to listen to changes and update other textfields. I dont have much choice on this matter as a lot of fields are dependent on each other. But the app is encountering a lot of performance issues. It freezes a lot. Can I get some tips on developing apps with insane number of textfields. I think all the textwatchers I use is slowing down the ui thread.

  //Business Operating Cost
        TextWatcher businessOperatingCostTextWatcher = new TextWatcher() {
            private double edtTaxPayment = 0.0;
            private double txtPaymentBusinessLoan = 0.0;
            private double edtOtherBusiness = 0.0;
            private double edtBasicServices = 0.0;
            private double edtTransport = 0.0;
            private double edtLocalRental = 0.0;
            private double edtSalaries = 0.0;
            private double unforeseen = 0.0;

            @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) {
                if (mCashFlowBinding.edtSalaries != null && mCashFlowBinding.edtSalaries.length() != 0) {
                    edtSalaries = Double.parseDouble(mCashFlowBinding.edtSalaries.getText().toString());
                }
                if (mCashFlowBinding.edtLocalRental != null && mCashFlowBinding.edtLocalRental.length() != 0) {
                    edtLocalRental = Double.parseDouble(mCashFlowBinding.edtLocalRental.getText().toString());
                }
                if (mCashFlowBinding.edtTransport != null && mCashFlowBinding.edtTransport.length() != 0) {
                    edtTransport = Double.parseDouble(mCashFlowBinding.edtTransport.getText().toString());
                }
                if (mCashFlowBinding.edtBasicServices != null && mCashFlowBinding.edtBasicServices.length() != 0) {
                    edtBasicServices = Double.parseDouble(mCashFlowBinding.edtBasicServices.getText().toString());
                }
                if (mCashFlowBinding.edtOtherBusiness != null && mCashFlowBinding.edtOtherBusiness.length() != 0) {
                    edtOtherBusiness = Double.parseDouble(mCashFlowBinding.edtOtherBusiness.getText().toString());
                }
                if (mCashFlowBinding.txtPaymentBusinessLoan != null && mCashFlowBinding.txtPaymentBusinessLoan.length() != 0) {
                    txtPaymentBusinessLoan = Double.parseDouble(mCashFlowBinding.txtPaymentBusinessLoan.getText().toString());
                }
                if (mCashFlowBinding.edtTaxPayment != null && mCashFlowBinding.edtTaxPayment.length() != 0) {
                    edtTaxPayment = Double.parseDouble(mCashFlowBinding.edtTaxPayment.getText().toString());
                }
                if (mCashFlowBinding.txtUnforseen != null && mCashFlowBinding.txtUnforseen.length() != 0) {
                    unforeseen = Double.parseDouble(mCashFlowBinding.txtUnforseen.getText().toString());
                }
                double resultSum = (edtTaxPayment + txtPaymentBusinessLoan + edtOtherBusiness + edtBasicServices + edtTransport + edtLocalRental + edtSalaries + unforeseen);

                if (!Double.isNaN(resultSum) && !Double.isInfinite(resultSum)) {
                    mCashFlowBinding.txtBusinessOperatingCost1.setText(String.valueOf(resultSum));
                } else {
                    mCashFlowBinding.txtBusinessOperatingCost1.setText("0.0");
                }
            }
        };

        mCashFlowBinding.edtSalaries.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtLocalRental.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtTransport.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtBasicServices.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtOtherBusiness.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.txtPaymentBusinessLoan.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtTaxPayment.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.txtUnforseen.addTextChangedListener(businessOperatingCostTextWatcher);
Phoenix
  • 59
  • 7
ACE
  • 1
  • 1
  • only one `TextWatcher` is activated at a time, so this might not be the reason. check if there are other work you are doing in the main thread – ruben Nov 11 '20 at 05:59

1 Answers1

0

What you could do to ease the Main UI Thread:

Expose some LiveData of your ViewModel to your View and just call postValue in your TextWatcher. It then quickly releases.

In the LiveData observer you can then handle the processing on a separate Thread and send the result again to a LiveData object finalOperstingCosts.

In the observer of this LiveData you then set the Text to the field.

This way you never have long running tasks on the Main Thread.

Tobi
  • 858
  • 7
  • 15