Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views5 pages

Calculator

The document contains XML and Java code for two Android applications: a calculator and a number input interface. The calculator allows users to perform basic arithmetic operations (addition, subtraction, multiplication, division) on two decimal numbers, while the number input interface enables users to click buttons representing digits 0-9, submit their input, and clear it. Both applications utilize Android's UI components and event handling to interact with users.

Uploaded by

yash67138
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)
25 views5 pages

Calculator

The document contains XML and Java code for two Android applications: a calculator and a number input interface. The calculator allows users to perform basic arithmetic operations (addition, subtraction, multiplication, division) on two decimal numbers, while the number input interface enables users to click buttons representing digits 0-9, submit their input, and clear it. Both applications utilize Android's UI components and event handling to interact with users.

Uploaded by

yash67138
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/ 5

Calculator

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 1"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/num1"
android:hint="Number 2"
android:inputType="numberDecimal" />

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/num2"
android:text="+" />

<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b1"
android:text="-" />

<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b2"
android:text="×" />

<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b3"
android:text="÷" />

<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b4"
android:layout_marginTop="10dp"
android:text="Result:"
android:textSize="16sp" />
</RelativeLayout>

JAVA

package com.msbte.calculator;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

EditText num1, num2;


TextView result;
Button btnAdd, btnSub, btnMul, btnDiv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
result = findViewById(R.id.txtResult);
btnAdd = findViewById(R.id.b1);
btnSub = findViewById(R.id.b2);
btnMul = findViewById(R.id.b3);
btnDiv = findViewById(R.id.b4);

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


btnSub.setOnClickListener(v -> calculate('-'));
btnMul.setOnClickListener(v -> calculate('*'));
btnDiv.setOnClickListener(v -> calculate('/'));
}

private void calculate(char operator) {


try {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = 0;

switch (operator) {
case '+': res = n1 + n2; break;
case '-': res = n1 - n2; break;
case '*': res = n1 * n2; break;
case '/':
if (n2 != 0) res = n1 / n2;
else {
result.setText("Cannot divide by zero");
return;
}
break;
}

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

} catch (NumberFormatException e) {
result.setText("Enter valid numbers");
}
}
}

button 0 TO 9 submit clrar


XML

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp" android:stretchColumns="*">

<TextView
android:id="@+id/display" android:text="Clicked: "
android:layout_height="wrap_content" android:layout_width="match_parent"
android:textSize="18sp" />

<!-- Rows of buttons -->


<TableRow>
<Button android:id="@+id/b1" android:text="1"/>
<Button android:id="@+id/b2" android:text="2"/>
<Button android:id="@+id/b3" android:text="3"/>
</TableRow>
<TableRow>
<Button android:id="@+id/b4" android:text="4"/>
<Button android:id="@+id/b5" android:text="5"/>
<Button android:id="@+id/b6" android:text="6"/>
</TableRow>
<TableRow>
<Button android:id="@+id/b7" android:text="7"/>
<Button android:id="@+id/b8" android:text="8"/>
<Button android:id="@+id/b9" android:text="9"/>
</TableRow>
<TableRow>
<Button android:id="@+id/b0" android:text="0"/>
<Button android:id="@+id/submit" android:text="Submit"/>
<Button android:id="@+id/clear" android:text="Clear"/>
</TableRow>
</TableLayout>

JAVA

package com.msbte.num0to9;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

StringBuilder data = new StringBuilder();


TextView display;
Button submit,clear;

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

display = findViewById(R.id.display);
submit=findViewById(R.id.submit);
clear=findViewById(R.id.clear);
int[] ids = {R.id.b0, R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8,
R.id.b9};
for (int id : ids)
{
findViewById(id).setOnClickListener(v ->
{
Button b = (Button) v;
data.append(b.getText());
});
}

submit.setOnClickListener(v -> display.setText("Clicked: " + data));


clear.setOnClickListener(v -> {
data.setLength(0);
display.setText("Clicked: ");
});
}
}

You might also like