Search code examples
androidandroid-layoutkotlinandroid-edittextaddtextchangedlistener

compute the edittext value using addTextChangedListener


i have a two editext and one textview...textview value is coming from api and based upon the value i have to estimate rest values...

textview value which is coming from api is tv_UOM_value first edittext id is tv_UOM_0 second edittext id is tv_UOM_value_0

two cases i have to handle:--

1)when i type value in tv_UOM_0 it should add tv_UOM_0 + tv_UOM_value and display in tv_UOM_value_0

2)when i type value in tv_UOM_value_0 it should subtract tv_UOM_value_0 - tv_UOM_value and display in tv_UOM_0

following is my code :-

 tv_UOM_0.addTextChangedListener(object:TextWatcher{
        @SuppressLint("SetTextI18n")
        override fun afterTextChanged(p0: Editable?) {
            if(p0?.isNotEmpty() == true){
                tv_UOM_value_0.setText(
                        "${tv_UOM_value.text.toString()
                                .toInt() + tv_UOM_0.text.toString().toInt()}"
                    )
            }else{
                tv_UOM_value_0.setText("")
            }
        }
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }
        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }
    })
        tv_UOM_value_0.addTextChangedListener(object:TextWatcher{
            @SuppressLint("SetTextI18n")
            override fun afterTextChanged(p0: Editable?) {
                if (p0?.isNotEmpty() == true) {
                        tv_UOM_0.setText(
                                "${tv_UOM_value_0.text.toString()
                                        .toInt() - tv_UOM_value.text.toString().toInt()}"
                        )
                    }
                else{
                    tv_UOM_0.setText("")
                }
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })

above code output:-device gets hang when i type value in first editext

need help thanks in advance...


Solution

  • Your issue is that your code is creating infinite loop where both EditTexts' listeners call each other when value is changed.

    There are two ways to handle this scenario:

    1. Unregistering & re-registering listener (Convenient way)

    Let's take an example here:

    During listener event of tv_UOM_0 (first edit text) when you want to change value to tv_UOM_value_0 (second edit text).

    So, before calling

    tv_UOM_value_0.setText("${tv_UOM_value.text.toString().toInt() + tv_UOM_0.text.toString().toInt()}")
    

    You should remove listener to second edit text using tv_UOM_value_0.removeTextChangedListener(textWatcherObject for tv_UOM_value_0); before if/else condition and then after if/else condition re-register listener again.

    Same goes for code of second edit text listener.


    2. Check whether EditText has focus (Straightforward way)

    During both the listeners you should check whether that given edit text has focus or not and simply return if there's no focus.

    Checkout the code below:

    // Listener method of tv_UOM_0
    @SuppressLint("SetTextI18n")
    override fun afterTextChanged(p0: Editable?) {
        if(!tv_UOM_0.hasFocus()) {
            return
        }
        if(p0?.isNotEmpty() == true){
            tv_UOM_value_0.setText("${tv_UOM_value.text.toString().toInt() + tv_UOM_0.text.toString().toInt()}")
        } else {
            tv_UOM_value_0.setText("")
        }
    }