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

Simple calc

The document contains an Android XML layout for a simple calculator application, featuring a display and a grid of buttons for numbers and operations. The accompanying Java code defines the MainActivity class, which handles button clicks, updates the display, performs calculations, and manages the current input and operator. The application supports basic arithmetic operations: addition, subtraction, multiplication, and division.

Uploaded by

shreyagowda206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Simple calc

The document contains an Android XML layout for a simple calculator application, featuring a display and a grid of buttons for numbers and operations. The accompanying Java code defines the MainActivity class, which handles button clicks, updates the display, performs calculations, and manages the current input and operator. The application supports basic arithmetic operations: addition, subtraction, multiplication, and division.

Uploaded by

shreyagowda206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

<!-- Display for showing numbers and result -->

<EditText

android:id="@+id/display"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

<!-- Buttons layout -->

<GridLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:columnCount="4"

android:orientation="horizontal"

android:rowCount="5">

<!-- Row 1 -->

<Button android:id="@+id/btn7"

android:text="7" />

<Button android:id="@+id/btn8"

android:text="8" />

<Button android:id="@+id/btn9"

android:text="9" />

<Button android:id="@+id/btnDivide"

android:text="/" />

<!-- Row 2 -->

<Button android:id="@+id/btn4"

android:text="4" />

<Button android:id="@+id/btn5"

android:text="5" />
<Button android:id="@+id/btn6"

android:text="6" />

<Button android:id="@+id/btnMultiply"

android:text="*" />

<!-- Row 3 -->

<Button android:id="@+id/btn1"

android:text="1" />

<Button android:id="@+id/btn2"

android:text="2" />

<Button android:id="@+id/btn3"

android:text="3" />

<Button android:id="@+id/btnSubtract"

android:text="-" />

<!-- Row 4 -->

<Button android:id="@+id/btn0"

android:text="0" />

<Button android:id="@+id/btnClear"

android:text="C" />

<Button android:id="@+id/btnEqual"

android:text="=" />

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

android:text="+" /></GridLayout>

</LinearLayout>

Java code

public class MainActivity extends AppCompatActivity {

private EditText display;

private String currentInput = "";

private String operator = "";

private double operand1 = 0;

private double operand2 = 0;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

display = findViewById(R.id.display);

// Number buttons

findViewById(R.id.btn0).setOnClickListener(v -> updateDisplay("0"));

findViewById(R.id.btn1).setOnClickListener(v -> updateDisplay("1"));

findViewById(R.id.btn2).setOnClickListener(v -> updateDisplay("2"));

findViewById(R.id.btn3).setOnClickListener(v -> updateDisplay("3"));

findViewById(R.id.btn4).setOnClickListener(v -> updateDisplay("4"));

findViewById(R.id.btn5).setOnClickListener(v -> updateDisplay("5"));

findViewById(R.id.btn6).setOnClickListener(v -> updateDisplay("6"));

findViewById(R.id.btn7).setOnClickListener(v -> updateDisplay("7"));

findViewById(R.id.btn8).setOnClickListener(v -> updateDisplay("8"));

findViewById(R.id.btn9).setOnClickListener(v -> updateDisplay("9"));

// Operator buttons

findViewById(R.id.btnAdd).setOnClickListener(v -> setOperator("+"));

findViewById(R.id.btnSubtract).setOnClickListener(v -> setOperator("-"));

findViewById(R.id.btnMultiply).setOnClickListener(v -> setOperator("*"));

findViewById(R.id.btnDivide).setOnClickListener(v -> setOperator("/"));

// Equal button

findViewById(R.id.btnEqual).setOnClickListener(v -> calculateResult());

// Clear button

findViewById(R.id.btnClear).setOnClickListener(v -> clearDisplay());

}private void updateDisplay(String value) {

currentInput += value;

display.setText(currentInput);

private void setOperator(String operator) {

if (!currentInput.isEmpty()) {
operand1 = Double.parseDouble(currentInput);

this.operator = operator;

currentInput = "";

private void calculateResult() {

if (!currentInput.isEmpty()) {

operand2 = Double.parseDouble(currentInput);

double result = 0;

switch (operator) {

case "+":

result = operand1 + operand2;

break;

case "-":

result = operand1 - operand2;

break;

case "*":

result = operand1 * operand2;

break;

case "/":

if (operand2 != 0) {

result = operand1 / operand2;

} else {

result = 0; // Avoid division by zero

break;

display.setText(String.valueOf(result));

currentInput = String.valueOf(result); // Store result for next operation

}
private void clearDisplay() {

currentInput = "";

operator = "";

display.setText("");

You might also like