Search code examples
androidkotlintextviewandroid-edittexttextwatcher

Multiplying 2 numbers without buttons in Kotlin TextWatcher


My aim is to automatically display the numbers entered by the user in the textView below using 2 edittext as shown below. How can I do this?Because this is crush. I would appreciate it if you explain in detail. Thanks in advance.

 items3=pronumber.text.toString() // Edittext
    items4=proprice.text.toString()// Edittext


    pronumber.addTextChangedListener ( object:TextWatcher{
        override fun afterTextChanged(p0: Editable?) {

        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            TODO("Not yet implemented")
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            totalprice.text= "Toplam Tutar : "+(items3.toInt()*items4.toInt()).toString() +" TL"  //TextView

        }


    } )

    proprice.addTextChangedListener ( object:TextWatcher{
        override fun afterTextChanged(p0: Editable?) {

        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            TODO("Not yet implemented")
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            totalprice.text= "Toplam Tutar : "+(items3.toInt()*items4.toInt()).toString() +" TL"  //TextView

        }


    } )


    items5=totalprice.text.toString()

Solution

  • You should change your code like

            items3=pronumber.text.toString() // Edittext
            items4=proprice.text.toString()// Edittext
    
    
            pronumber.addTextChangedListener ( object:TextWatcher{
                override fun afterTextChanged(p0: Editable?) {
    
                }
    
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }
    
                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    val first = pronumber.text.toString()
                    val second = proprice.text.toString()
                    totalprice.text =  if (first.isBlank() || second.isBlank()){
                        "Toplam Tutar : 0 TL"
                    } else {
                        "Toplam Tutar : ${first.toInt().times(second.toInt())} TL"
                    }
    
                }
    
    
            } )
    
            proprice.addTextChangedListener ( object:TextWatcher{
                override fun afterTextChanged(p0: Editable?) {
    
                }
    
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }
    
                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    totalprice.text =  if (first.isBlank() || second.isBlank()){
                        "Toplam Tutar : 0 TL"
                    } else {
                        "Toplam Tutar : ${first.toInt().times(second.toInt())} TL"
                    }
                }
    
    
            } )
    
    
            items5=totalprice.text.toString()