0% found this document useful (0 votes)
12 views4 pages

Calc Mad

The document contains an Android layout XML and Java code for a simple calculator application. It includes two EditText fields for number input, four buttons for arithmetic operations, and a TextView to display results. The Java code handles button clicks to perform calculations and display the results, including error handling for empty inputs and division by zero.

Uploaded by

xyz935613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Calc Mad

The document contains an Android layout XML and Java code for a simple calculator application. It includes two EditText fields for number input, four buttons for arithmetic operations, and a TextView to display results. The Java code handles button clicks to perform calculations and display the results, including error handling for empty inputs and division by zero.

Uploaded by

xyz935613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<?xml version="1.0" encoding="utf-8"?

>
<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;

public class MainActivity extends AppCompatActivity {

EditText etNumber1, etNumber2;


Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 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);

// Button Click Listeners


btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('+');
}
});

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('/');
}
});
}

private void calculate(char operator) {


String num1 = etNumber1.getText().toString();
String num2 = etNumber2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
tvResult.setText("Please enter both numbers!");
return;
}

double number1 = Double.parseDouble(num1);


double number2 = Double.parseDouble(num2);
double result = 0;

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;
}

tvResult.setText("Result: " + (result == Math.floor(result) ? String.valueOf((int) result) :


String.valueOf(result)));

}
}

You might also like