0% found this document useful (0 votes)
4 views6 pages

jaymad9.2

The document contains the XML layout for an Android calculator app and the corresponding Java code for its functionality. The layout includes buttons for numbers, operations, and a display area for results. The Java code handles user interactions, performs calculations, and updates the display based on user input.
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)
4 views6 pages

jaymad9.2

The document contains the XML layout for an Android calculator app and the corresponding Java code for its functionality. The layout includes buttons for numbers, operations, and a display area for results. The Java code handles user interactions, performs calculations, and updates the display based on user input.
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/ 6

Activity_main.

xml

<?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:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:gravity="end"
android:padding="16dp"
android:textSize="30sp"
android:text="0"
android:layout_marginBottom="16dp"/>

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

<Button
android:id="@+id/buttonClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
android:textSize="20sp" />

<Button
android:id="@+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="20sp" />
</LinearLayout>

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

<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="20sp" />

<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="20sp" />

<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="20sp" />

<Button
android:id="@+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="20sp" />
</LinearLayout>

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

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="20sp" />

<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="20sp" />

<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="20sp" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="20sp" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="20sp" />

<Button
android:id="@+id/buttonAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="20sp" />
</LinearLayout>

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

<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="0"
android:textSize="20sp" />

<Button
android:id="@+id/buttonDot"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="20sp" />
<Button
android:id="@+id/buttonEquals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="20sp" />
</LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.cal;

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 textViewResult;


private String currentNumber = "";
private String leftOperand = "";
private String currentOperator = "";
private boolean operatorPressed = false;

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

textViewResult = findViewById(R.id.textViewResult);

// Number buttons
setupNumberButton(R.id.button0, "0");
setupNumberButton(R.id.button1, "1");
setupNumberButton(R.id.button2, "2");
setupNumberButton(R.id.button3, "3");
setupNumberButton(R.id.button4, "4");
setupNumberButton(R.id.button5, "5");
setupNumberButton(R.id.button6, "6");
setupNumberButton(R.id.button7, "7");
setupNumberButton(R.id.button8, "8");
setupNumberButton(R.id.button9, "9");
setupNumberButton(R.id.buttonDot, ".");

// Operation buttons
setupOperatorButton(R.id.buttonAdd, "+");
setupOperatorButton(R.id.buttonSubtract, "-");
setupOperatorButton(R.id.buttonMultiply, "*");
setupOperatorButton(R.id.buttonDivide, "/");

// Equals button
Button buttonEquals = findViewById(R.id.buttonEquals);
buttonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateResult();
}
});

// Clear button
Button buttonClear = findViewById(R.id.buttonClear);
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber = "";
leftOperand = "";
currentOperator = "";
operatorPressed = false;
textViewResult.setText("0");
}
});
}

private void setupNumberButton(int buttonId, final String number) {


Button button = findViewById(buttonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operatorPressed) {
currentNumber = "";
operatorPressed = false;
}

// Handle decimal point


if (number.equals(".") && currentNumber.contains(".")) {
return;
}

currentNumber += number;
textViewResult.setText(currentNumber);
}
});
}

private void setupOperatorButton(int buttonId, final String operator) {


Button button = findViewById(buttonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.isEmpty()) {
if (!leftOperand.isEmpty() && !operatorPressed) {
calculateResult();
}
leftOperand = currentNumber;
currentOperator = operator;
operatorPressed = true;
}
}
});
}

private void calculateResult() {


if (!leftOperand.isEmpty() && !currentNumber.isEmpty() && !currentOperator.isEmpty()) {
double result = 0;
double leftValue = Double.parseDouble(leftOperand);
double rightValue = Double.parseDouble(currentNumber);

switch (currentOperator) {
case "+":
result = leftValue + rightValue;
break;
case "-":
result = leftValue - rightValue;
break;
case "*":
result = leftValue * rightValue;
break;
case "/":
if (rightValue != 0) {
result = leftValue / rightValue;
} else {
textViewResult.setText("Error");
currentNumber = "";
leftOperand = "";
currentOperator = "";
operatorPressed = false;
return;
}
break;
}

// Convert to string, removing decimal point for whole numbers


String resultStr = (result == (long) result)
? String.valueOf((long) result)
: String.valueOf(result);

textViewResult.setText(resultStr);
leftOperand = resultStr;
currentNumber = resultStr;
currentOperator = "";
}
}
}

You might also like