0% found this document useful (0 votes)
14 views

Simple Calculator Android

The document provides the code for a simple Android calculator application using Java and XML. It includes an XML layout for user input and buttons for basic arithmetic operations, as well as Java code for handling button clicks and performing calculations. The application checks for valid input and handles division by zero errors.

Uploaded by

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

Simple Calculator Android

The document provides the code for a simple Android calculator application using Java and XML. It includes an XML layout for user input and buttons for basic arithmetic operations, as well as Java code for handling button clicks and performing calculations. The application checks for valid input and handles division by zero errors.

Uploaded by

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

Android Simple Calculator (Java & XML)

1. XML Layout (activity_main.xml)

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


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

<EditText
android:id="@+id/edtNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/edtNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />

<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="20sp"
android:padding="10dp"/>

<Button android:id="@+id/btnAdd" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Add" />
<Button android:id="@+id/btnSubtract" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Subtract" />
<Button android:id="@+id/btnMultiply" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Multiply" />
<Button android:id="@+id/btnDivide" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Divide" />
</LinearLayout>

2. Java Code (MainActivity.java)

package com.example.simplecalculator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText edtNumber1, edtNumber2;


TextView txtResult;
Button btnAdd, btnSubtract, btnMultiply, btnDivide;

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

edtNumber1 = findViewById(R.id.edtNumber1);
edtNumber2 = findViewById(R.id.edtNumber2);
txtResult = findViewById(R.id.txtResult);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);

btnAdd.setOnClickListener(v -> calculate('+'));


btnSubtract.setOnClickListener(v -> calculate('-'));
btnMultiply.setOnClickListener(v -> calculate('*'));
btnDivide.setOnClickListener(v -> calculate('/'));
}

private void calculate(char operation) {


String num1 = edtNumber1.getText().toString();
String num2 = edtNumber2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(this, "Please enter both numbers", Toast.LENGTH_SHORT).show();
return;
}

double number1 = Double.parseDouble(num1);


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

switch (operation) {
case '+': result = number1 + number2; break;
case '-': result = number1 - number2; break;
case '*': result = number1 * number2; break;
case '/':
if (number2 == 0) {
Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
result = number1 / number2;
break;
}

txtResult.setText("Result: " + result);


}
}

You might also like