0% found this document useful (0 votes)
17 views9 pages

22BCS002 Mad

The document outlines the development of a simple calculator app using Android components to perform basic arithmetic operations. It details the steps for creating a new project, designing the user interface, implementing functionality in Java, and testing the application. The app includes features for addition, subtraction, multiplication, and division, displaying results in a TextView.

Uploaded by

ABIRAMI M
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)
17 views9 pages

22BCS002 Mad

The document outlines the development of a simple calculator app using Android components to perform basic arithmetic operations. It details the steps for creating a new project, designing the user interface, implementing functionality in Java, and testing the application. The app includes features for addition, subtraction, multiplication, and division, displaying results in a TextView.

Uploaded by

ABIRAMI M
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

ABIRAMI M 22BCS002

U18CSI5205L – MOBILE APPLICATION


DEVELOPMENT USING ANDROID - 3a
Name: ABIRAMI M
Roll: 22BCS002
AIM
To develop a simple calculator app using Android components
such as TextView, EditText, and Button to perform basic
arithmetic operations: Addition, Subtraction, Multiplication, and
Division.

EXPERIMENTAL STEPS
Step 1: Create a New Android Project
1. Open Android Studio.
2. Create a New Project with an Empty Activity.
3. Set the language to Java.
Step 2: Design the User Interface (activity_main.xml)
1. Use a LinearLayout (vertical orientation) to arrange UI
components.
2. Add two EditText fields to accept user input.
3. Include four Button components for arithmetic operations
(+, -, ×, ÷).
4. Add a TextView to display the result.
Step 3: Implement Functionality in [Link]
1. Retrieve values from EditText fields.
2. Set click listeners for each button.
3. Perform the respective arithmetic operation when a button
is clicked.
4. Display the result in the TextView.
Step 4: Run and Test the Application
1. Build and run the application on an emulator or a physical
device.
2. Enter two numbers and test all arithmetic operations.

1
ABIRAMI M 22BCS002

Xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp"
android:gravity="center"
tools:context=".MainActivity">

<!-- Title Text -->


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simple Calculator"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="center"/>

<!-- First Number Input -->


<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:textSize="20sp"
android:inputType="numberDecimal"/>

2
ABIRAMI M 22BCS002

<!-- Second Number Input -->


<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:textSize="20sp"
android:inputType="numberDecimal"/>

<!-- Buttons for Operations -->


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

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

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

3
ABIRAMI M 22BCS002

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

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

<!-- Display Result -->


<TextView
android:id="@+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>

JAVA CODE:
package [Link];

import [Link];

4
ABIRAMI M 22BCS002

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity implements
[Link] {
// Declare UI elements
Button buttonAdd, buttonDiff, buttonMul, buttonDiv;
EditText editTextN1, editTextN2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize buttons, EditTexts, and TextView
buttonAdd = findViewById([Link].btn_add);
buttonDiff = findViewById([Link].btn_diff); // Fixed incorrect ID reference
buttonMul = findViewById([Link].btn_mul);
buttonDiv = findViewById([Link].btn_div);
editTextN1 = findViewById([Link].number1);
editTextN2 = findViewById([Link].number2);
textView = findViewById([Link]);
// Set click listeners for buttons
[Link](this);
[Link](this);
[Link](this);
[Link](this); }
public Integer getIntFromEdit(EditText editText) {
String input = [Link]().toString().trim();
if ([Link]()) {

5
ABIRAMI M 22BCS002

[Link](this, "Please enter a number",


Toast.LENGTH_SHORT).show();
return null; // Return null instead of 0 to prevent false calculations
}
try {
return [Link](input); // Parse integer safely
} catch (NumberFormatException e) {
[Link](this, "Invalid input! Please enter a valid number.",
Toast.LENGTH_SHORT).show();
return null;
}
}
@Override
public void onClick(View view) {
Integer num1 = getIntFromEdit(editTextN1);
Integer num2 = getIntFromEdit(editTextN2);
if (num1 == null || num2 == null) return;
int result = 0; // To store the result
// Logic for button clicks
switch ([Link]()) {
case [Link].btn_add:
result = num1 + num2;
break;
case [Link].btn_diff:
result = num1 - num2;
break;
case [Link].btn_mul:
result = num1 * num2;
break;
case [Link].btn_div:
if (num2 == 0) {
[Link](this, "Cannot divide by zero",
Toast.LENGTH_SHORT).show();

6
ABIRAMI M 22BCS002

return; // Prevent further execution


}
result = num1 / num2;
break;
}
// Display the result
[Link]("Result: " + result);
}
}

ADDITION:

7
ABIRAMI M 22BCS002

SUBTRACTION:

MULTIPLICATION:

8
ABIRAMI M 22BCS002

DIVISION:

CONCLUSION:
A simple calculator app was successfully developed using TextView,
EditText, and Button perform basic arithmetic operations

You might also like