Calc Mad
Calc Mad
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<EditText
android:id="@+id/etNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"/>
<EditText
android:id="@+id/etNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:layout_below="@id/etNumber1"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_below="@id/etNumber2"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btnSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_below="@id/etNumber2"
android:layout_toRightOf="@id/btnAdd"
android:layout_marginLeft="10dp"/>
<Button
android:id="@+id/btnMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="×"
android:layout_below="@id/btnAdd"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btnDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="÷"
android:layout_below="@id/btnSubtract"
android:layout_toRightOf="@id/btnMultiply"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
android:textStyle="bold"
android:layout_below="@id/btnMultiply"
android:layout_marginTop="20dp"/>
</RelativeLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
// Initialize Views
etNumber1 = findViewById(R.id.etNumber1);
etNumber2 = findViewById(R.id.etNumber2);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
tvResult = findViewById(R.id.tvResult);
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('-');
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('*');
}
});
btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('/');
}
});
}
if (num1.isEmpty() || num2.isEmpty()) {
tvResult.setText("Please enter both numbers!");
return;
}
switch (operator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
if (number2 == 0) {
tvResult.setText("Cannot divide by zero!");
return;
}
result = number1 / number2;
break;
}
}
}