androidkotlinandroid-button

setOnClickListener doesnt work when clicking the button


So I am trying to build a small calculator app in Android Studio using Kotlin. When the user presses the add button both the numbers should be calculated and displayed in the textView but when I press the button nothing happens. Please help me resolve this.

I am getting unresolved references error please have a look

MainActivity.kt

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(activity_main)
    addButton.setOnClickListener {
        addition()
    }
}
fun addition() {
    val num1Text = numberFirst.toString()
    val num2Text = numberSecond.toString()
    // Check if the input is empty
    if (num1Text.isEmpty() || num2Text.isEmpty()) {
        resultView = "Please enter valid numbers".toInt()
        return
    }
    // Convert the input to integers and perform addition
    val num1 = num1Text.toInt()
    val num2 = num2Text.toInt()
    val sum = num1 + num2
    resultView = "Result: $sum".toInt() // Display the result in 
    the TextView
   }
  }

This is the activity main file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/textView4"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:text="Simple Calculator App"
    android:textAlignment="center"
    android:textSize="30dp" />

<EditText
    android:ems="10"
    android:hint="Enter first number"
    android:id="@+id/numberFirst"
    android:inputType="number"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="20dp"
    android:layout_width="300dp" />

<EditText
    android:ems="5"
    android:hint="Enter second number"
    android:id="@+id/numberSecond"
    android:inputType="number"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="20dp"
    android:layout_width="300dp" />

<TextView
    android:id="@+id/resultView"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"

    android:textSize="25dp" />

<LinearLayout
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/addButton"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:layout_width="120dp"
        android:text="+"
        android:textSize="25dp" />
</LinearLayout>

Solution

  • Try this>

    class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        val addButton = findViewById<Button>(R.id.addButton) // Button element with ID addButton
    
        addButton.setOnClickListener {
            addition()
        }
    }
    
    fun addition() {
        val num1Text = findViewById<EditText>(R.id.numberFirst).text.toString() // EditText with ID numberFirst
        val num2Text = findViewById<EditText>(R.id.numberSecond).text.toString() // EditText with ID numberSecond
        val resultView = findViewById<TextView>(R.id.resultView) // TextView with ID resultView
    
        if (num1Text.isEmpty() || num2Text.isEmpty()) {
            resultView.text = "Please enter valid numbers"
            return
        }
    
        val num1 = num1Text.toInt()
        val num2 = num2Text.toInt()
        val sum = num1 + num2
    
        resultView.text = "Result: $sum"
       }
     }