In this tutorial, we will walk through the complete process to create a simple calculator in android studio using Java. This application will handle basic arithmetic operations like addition, subtraction, multiplication, and division, along with a functional Clear (C) button to reset the calculator state.
First, open Android Studio and create a new project. Select the Empty Views Activity (or Empty Activity depending on your version) template, name your project SimpleCalculator, select Java as the language, and click Finish.
To avoid repeating the same layout attributes for every single button, we will define a reusable style. Open your res/values/themes.xml (or styles.xml) file and paste the following style inside the <resources> tag:
<!-- Custom style for Calculator Buttons -->
<style name="CalculatorButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:textSize">24sp</item>
<item name="android:layout_margin">4dp</item>
<item name="android:background">?android:attr/selectableItemBackground</item>
</style>
Now, open res/layout/activity_main.xml. We will use a LinearLayout as the root view containing a TextView for display, and a GridLayout to arrange our keys neatly in rows and columns. Replace the file content with this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#0A6056"
android:padding="16dp">
<!-- Display screen for results -->
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:textSize="32sp"
android:gravity="end"
android:padding="16dp"
android:background="#EEEEEE"/>
<!-- Grid container for buttons -->
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="5"
android:columnCount="4"
android:padding="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp">
<!-- Row 1 -->
<Button android:text="7" android:id="@+id/button7" style="@style/CalculatorButton"/>
<Button android:text="8" android:id="@+id/button8" style="@style/CalculatorButton"/>
<Button android:text="9" android:id="@+id/button9" style="@style/CalculatorButton"/>
<Button android:text="/" android:id="@+id/buttonDivide" style="@style/CalculatorButton"/>
<!-- Row 2 -->
<Button android:text="4" android:id="@+id/button4" style="@style/CalculatorButton"/>
<Button android:text="5" android:id="@+id/button5" style="@style/CalculatorButton"/>
<Button android:text="6" android:id="@+id/button6" style="@style/CalculatorButton"/>
<Button android:text="*" android:id="@+id/buttonMultiply" style="@style/CalculatorButton"/>
<!-- Row 3 -->
<Button android:text="1" android:id="@+id/button1" style="@style/CalculatorButton"/>
<Button android:text="2" android:id="@+id/button2" style="@style/CalculatorButton"/>
<Button android:text="3" android:id="@+id/button3" style="@style/CalculatorButton"/>
<Button android:text="-" android:id="@+id/buttonSubtract" style="@style/CalculatorButton"/>
<!-- Row 4 -->
<Button android:text="0" android:id="@+id/button0" style="@style/CalculatorButton"/>
<Button android:text="." android:id="@+id/buttonDot" style="@style/CalculatorButton"/>
<Button android:text="=" android:id="@+id/buttonEqual" style="@style/CalculatorButton"/>
<Button android:text="+" android:id="@+id/buttonAdd" style="@style/CalculatorButton"/>
<!-- Clear Button -->
<Button
android:id="@+id/buttonClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="C"
android:textSize="24sp"
android:layout_margin="4dp"/>
</GridLayout>
</LinearLayout>
Finally, let's connect the views to our Java logic. Open MainActivity.java and add code to register click event listeners, manage state expressions, and perform mathematical parsing smoothly:
package com.example.simplecalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView resultText;
private String currentInput = "";
private double firstNumber = 0.0;
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = findViewById(R.id.resultText);
// Initializing click event handlers
setNumericButtonClickListener();
setOperatorButtonClickListener();
// Clear button logic configuration
findViewById(R.id.buttonClear).setOnClickListener(v -> {
currentInput = "";
firstNumber = 0.0;
operator = "";
resultText.setText("0");
});
}
private void setNumericButtonClickListener() {
View.OnClickListener listener = v -> {
Button button = (Button) v;
currentInput += button.getText().toString();
resultText.setText(currentInput);
};
findViewById(R.id.button0).setOnClickListener(listener);
findViewById(R.id.button1).setOnClickListener(listener);
findViewById(R.id.button2).setOnClickListener(listener);
findViewById(R.id.button3).setOnClickListener(listener);
findViewById(R.id.button4).setOnClickListener(listener);
findViewById(R.id.button5).setOnClickListener(listener);
findViewById(R.id.button6).setOnClickListener(listener);
findViewById(R.id.button7).setOnClickListener(listener);
findViewById(R.id.button8).setOnClickListener(listener);
findViewById(R.id.button9).setOnClickListener(listener);
findViewById(R.id.buttonDot).setOnClickListener(listener);
}
private void setOperatorButtonClickListener() {
View.OnClickListener operatorListener = v -> {
Button button = (Button) v;
if (!currentInput.isEmpty()) {
firstNumber = Double.parseDouble(currentInput);
operator = button.getText().toString();
currentInput = "";
}
};
findViewById(R.id.buttonAdd).setOnClickListener(operatorListener);
findViewById(R.id.buttonSubtract).setOnClickListener(operatorListener);
findViewById(R.id.buttonMultiply).setOnClickListener(operatorListener);
findViewById(R.id.buttonDivide).setOnClickListener(operatorListener);
// Equal button logic configuration
findViewById(R.id.buttonEqual).setOnClickListener(v -> {
if (!currentInput.isEmpty()) {
double secondNumber = Double.parseDouble(currentInput);
double result = 0.0;
switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
// Prevent app from crashing due to division by zero
result = secondNumber != 0 ? firstNumber / secondNumber : 0;
break;
default:
return;
}
resultText.setText(String.valueOf(result));
currentInput = "";
}
});
}
}
Now hit the Run icon inside Android Studio to test the app on a connected hardware device or Virtual Device emulator. Ensure all components map effectively without generating unexpected Logcat exceptions.