0% found this document useful (0 votes)
6 views10 pages

Calculator App

The document contains XML and Java code for a simple calculator app. The XML defines the user interface with input fields for two numbers and buttons for addition, subtraction, multiplication, and division. The Java code implements the logic for performing calculations and displaying results based on user input.

Uploaded by

riyatorane1234
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)
6 views10 pages

Calculator App

The document contains XML and Java code for a simple calculator app. The XML defines the user interface with input fields for two numbers and buttons for addition, subtraction, multiplication, and division. The Java code implements the logic for performing calculations and displaying results based on user input.

Uploaded by

riyatorane1234
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/ 10

XML Code:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simple Calculator"
android:textSize="32sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="24dp" />

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number 1"
android:inputType="number"
android:textSize="20sp"
android:layout_marginBottom="12dp" />

<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number 2"
android:inputType="number"
android:textSize="20sp"
android:layout_marginBottom="12dp" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginBottom="24dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<Button
android:id="@+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+"
android:onClick="doSum"
android:textSize="24sp"
android:layout_margin="8dp" />

<Button
android:id="@+id/sub"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="-"
android:onClick="doSub"
android:textSize="24sp"
android:layout_margin="8dp" />

<Button
android:id="@+id/mul"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="×"
android:onClick="doMul"
android:textSize="24sp"
android:layout_margin="8dp" />

<Button
android:id="@+id/div"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="÷"
android:onClick="doDiv"
android:textSize="24sp"
android:layout_margin="8dp" />
</LinearLayout>
</LinearLayout>

Java Code:
package com.example.calculatorapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


EditText e1, e2;
TextView result;
int n1, n2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.e1);
e2 = findViewById(R.id.e2);
result = findViewById(R.id.result);
}

public boolean getNum() {


String s1 = e1.getText().toString().trim();
String s2 = e2.getText().toString().trim();

if (s1.isEmpty() || s2.isEmpty()) {
result.setText("Please enter both numbers");
return false;
}

try {
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s2);
} catch (NumberFormatException e) {
result.setText("Invalid input");
return false;
}

return true;
}

public void doSum(View v) {


if (getNum()) {
int sum = n1 + n2;
result.setText("Result: " + sum);
}
}

public void doSub(View v) {


if (getNum()) {
int sub = n1 - n2;
result.setText("Result: " + sub);
}
}

public void doMul(View v) {


if (getNum()) {
int mul = n1 * n2;
result.setText("Result: " + mul);
}
}

public void doDiv(View v) {


if (getNum()) {
if (n2 == 0) {
result.setText("Cannot divide by zero");
} else {
double div = (double) n1 / n2;
result.setText("Result: " + div);
}
}
}
}

Output:
Addition
Substraction
Multiplication
Division

You might also like