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