skip to Main Content
 val btnAdd = findViewById<Button>(R.id.btnAdd)
        val btnSub = findViewById<Button>(R.id.btnSub)
        btnAdd.setOnClickListener {
            val tx1 = findViewById<TextInputEditText>(R.id.tx1).toString().toInt()
            val tx2 = findViewById<TextInputEditText>(R.id.tx2).toString().toInt()

            var result = findViewById<TextView>(R.id.result)

            result.text = (tx1 + tx2).toString()
        }

I want to sum tx1 and tx2
but i get this message
Do not concatenate text displayed with setText. Use resource string with placeholders.
thanks

To find some help here!

2

Answers


  1. Get text of EditText before converting it to toString()

     val btnAdd = findViewById<Button>(R.id.btnAdd)
            val btnSub = findViewById<Button>(R.id.btnSub)
            btnAdd.setOnClickListener {
                val tx1 = findViewById<TextInputEditText>(R.id.tx1).text.toString().trim().toInt()
                val tx2 = findViewById<TextInputEditText>(R.id.tx2).text.toString().trim().toInt()
    
                var result = findViewById<TextView>(R.id.result)
    
                result.text = (tx1 + tx2).toString()
            }
    
    Login or Signup to reply.
  2. Please, to retrieve the value inside an EditText you need to get the text

    findViewById<TextInputEditText>(R.id.tx1).text.toString().toInt()
    

    Second, to resolve the warning just use a String res like this:

    <string name="my_string_res">My String with placeholder %d</string>
    

    and use it in this way:

    val sum = tx1 + tx2
    result.text = getString(R.string. my_string_res, sum)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search