You are on page 1of 12

Gauri Sharma SAP Id: 70411118032 Roll No: I228

Experiment No. 02
Name of Student: Gauri Sharma
Subject: MAD
Roll No. : I228
Batch: B
Performance Date: 10/12/20
Part A
Aim: Design an Android application (basic calculator) that performs basic arithmetic
applications like Addition, Subtraction etc, using textfield, button and the Toast. (CO2)
Prerequisite: Basic knowledge of
Java Programming Language
Basics of XML
Outcome:
 How to Drag the component or write the code for UI in activity_main.xml
 How to perform action on button using different types such as calling listener
on button or adding onClick property of button in activity's xml file.

Theory:

Android Button Example with Listener


Here, we are going to create two textfields and one button for sum of two numbers. If
user clicks button, sum of two input values is displayed on the Toast.

We can perform action on button using different types such as calling listener on
button or adding onClick property of button in activity's xml file.

1. button.setOnClickListener(new View.OnClickListener() {  
2.             @Override  
3.             public void onClick(View view) {  
4.                //code  
5.             }  
6. });  

1. <Button  
2.         android:onClick="methodName"  
3. />  

Drag the component or write the code for UI in


activity_main.xml
First of all, drag 2 textfields from the Text Fields palette and one button from the Form
Widgets palette as shown in the following figure.

1
Gauri Sharma SAP Id: 70411118032 Roll No: I228

The generated code for the ui components will be like this:

File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>  
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
4.     xmlns:tools="http://schemas.android.com/tools"  
5.     android:layout_width="match_parent"  
6.     android:layout_height="match_parent"  
7.     tools:context=".MainActivity">  
8.   
9.     <EditText  
10.         android:id="@+id/editText1"  
11.         android:layout_width="wrap_content"  
12.         android:layout_height="wrap_content"  
13.         android:layout_alignParentTop="true"  
14.         android:layout_centerHorizontal="true"  
15.         android:layout_marginTop="61dp"  
16.         android:ems="10"  
17.         android:inputType="number"  
18.         tools:layout_editor_absoluteX="84dp"  

2
Gauri Sharma SAP Id: 70411118032 Roll No: I228

19.         tools:layout_editor_absoluteY="53dp" />  
20.   
21.     <EditText  
22.         android:id="@+id/editText2"  
23.         android:layout_width="wrap_content"  
24.         android:layout_height="wrap_content"  
25.         android:layout_below="@+id/editText1"  
26.         android:layout_centerHorizontal="true"  
27.         android:layout_marginTop="32dp"  
28.         android:ems="10"  
29.         android:inputType="number"  
30.         tools:layout_editor_absoluteX="84dp"  
31.         tools:layout_editor_absoluteY="127dp" />  
32.   
33.     <Button  
34.         android:id="@+id/button"  
35.         android:layout_width="wrap_content"  
36.         android:layout_height="wrap_content"  
37.         android:layout_below="@+id/editText2"  
38.         android:layout_centerHorizontal="true"  
39.         android:layout_marginTop="109dp"  
40.         android:text="ADD"  
41.         tools:layout_editor_absoluteX="148dp"  
42.         tools:layout_editor_absoluteY="266dp" />  
43. </RelativeLayout>  

Activity class
Now write the code to display the sum of two numbers.

File: MainActivity.java
Import required packages
1. public class MainActivity extends AppCompatActivity {  
2.     private EditText edittext1, edittext2;  
3.     private Button buttonSum;  
4.   
5.     @Override  
6.     protected void onCreate(Bundle savedInstanceState) {  
7.         super.onCreate(savedInstanceState);  
8.         setContentView(R.layout.activity_main);  
9.   
10.         addListenerOnButton();  
11.     }  
12.   
13.     public void addListenerOnButton() {  

3
Gauri Sharma SAP Id: 70411118032 Roll No: I228

14.         edittext1 = (EditText) findViewById(R.id.editText1);  
15.         edittext2 = (EditText) findViewById(R.id.editText2);  
16.         buttonSum = (Button) findViewById(R.id.button);  
17.   
18.         buttonSum.setOnClickListener(new View.OnClickListener() {  
19.             @Override  
20.             public void onClick(View view) {  
21.                 String value1=edittext1.getText().toString();  
22.                 String value2=edittext2.getText().toString();  
23.                 int a=Integer.parseInt(value1);  
24.                 int b=Integer.parseInt(value2);  
25.                 int sum=a+b;  
26.                 Toast.makeText(getApplicationContext(),String.valueOf(sum), Toast.LENGT
H_LONG).show();  
27.             }  
28.         });  
29.     }  

Instructions:
1. Design the Application as shown in fig. using mainactivity.xml file
2. Use two edittext controls to collect input numbers for arithmetic operation
3. Use Button to submit the input and call the function that performs arithmetic
ie.addition in this case.
4. Add other buttons for remaining arithmetic buttons.

4
Gauri Sharma SAP Id: 70411118032 Roll No: I228

Part B

Code of xml file as well as java file


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1"
android:layout_width="206dp"
android:layout_height="46dp"
android:ems="10"
android:hint="Enter number1"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15" />

<EditText
android:id="@+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter number2"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.252"
android:autofillHints="" />

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.537" />

<Button
android:id="@+id/subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

5
Gauri Sharma SAP Id: 70411118032 Roll No: I228

android:text="Subtract"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.629" />

<Button
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.723" />

<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.815" />

<TextView
android:id="@+id/result"
android:layout_width="112dp"
android:layout_height="34dp"
android:text="Result"
android:textSize="23sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.411" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
ackage com.example.mycalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView result;
EditText number1, number2;
Button add, subtract, multiply, divide;

6
Gauri Sharma SAP Id: 70411118032 Roll No: I228

float result_num,num1,num2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView)findViewById(R.id.result);
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
add = (Button)findViewById(R.id.add);
subtract = (Button)findViewById(R.id.subtract);
multiply = (Button)findViewById(R.id.multiply);
divide = (Button)findViewById(R.id.divide);

add.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

num1 = Float.parseFloat(number1.getText().toString());
num2 = Float.parseFloat(number2.getText().toString());
result_num = num1 + num2;
result.setText(String.valueOf(result_num));
}
});
subtract.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

num1 = Float.parseFloat(number1.getText().toString());
num2 = Float.parseFloat(number2.getText().toString());
result_num = num1 - num2;
result.setText(String.valueOf(result_num));
}
});
multiply.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

num1 = Float.parseFloat(number1.getText().toString());
num2 = Float.parseFloat(number2.getText().toString());
result_num = num1 * num2;
result.setText(String.valueOf(result_num));
}
});
divide.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

num1 = Float.parseFloat(number1.getText().toString());
num2 = Float.parseFloat(number2.getText().toString());
result_num = num1 / num2;
result.setText(String.valueOf(result_num));
}
});

}
}

7
Gauri Sharma SAP Id: 70411118032 Roll No: I228

O/P:-
Addition:

Subtraction:

8
Gauri Sharma SAP Id: 70411118032 Roll No: I228

Multiply:

9
Gauri Sharma SAP Id: 70411118032 Roll No: I228

Divide:

10
Gauri Sharma SAP Id: 70411118032 Roll No: I228

11
Gauri Sharma SAP Id: 70411118032 Roll No: I228

Questions:

1. What is Application?
Answer:
An application, or application program, is a software program that runs on your
computer. Web browsers, e-mail programs, word processors, games, and utilities are
all applications. The word "application" is used because each program has a
specific application for the user.
2. What is Android?
Answer:
Android is a mobile operating system based on a modified version of the Linux kernel and
other open source software, designed primarily for touchscreen mobile devices such as
smartphones and tablets.
3. What is the Android Application Architecture?
Answer:
Android architecture is a software stack of components to support mobile device
needs. Android software stack contains a Linux Kernel, collection of c/c++ libraries
which are exposed through an application framework services, runtime,
and application.

Observation & Learning:


We designed an Android application (basic calculator) that performs basic arithmetic
applications like Addition, Subtraction etc, using textfield, button and the Toast.
Grade: Signature:

12

You might also like