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

I010 Practical1 MAD

The document describes an Android app development experiment to create a basic calculator app. The app uses a relative layout and linear layouts to display two number input fields and buttons for addition, subtraction, multiplication and division operations. When a button is clicked, the onClick listener in the MainActivity Java code performs the respective operation on the two numbers and displays the output in a text field. The student has provided the XML layout files, Java code for the MainActivity class, strings and colors resource files as well as screenshots of the app performing addition, subtraction and multiplication operations.

Uploaded by

Deepak Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

I010 Practical1 MAD

The document describes an Android app development experiment to create a basic calculator app. The app uses a relative layout and linear layouts to display two number input fields and buttons for addition, subtraction, multiplication and division operations. When a button is clicked, the onClick listener in the MainActivity Java code performs the respective operation on the two numbers and displays the output in a text field. The student has provided the XML layout files, Java code for the MainActivity class, strings and colors resource files as well as screenshots of the app performing addition, subtraction and multiplication operations.

Uploaded by

Deepak Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Part A

AIM: Android Layouts (Linear & Relative Layout) and Signal Handler.

Scenario: Create a Calculator using relative layout and Linear Layout for the buttons, which would accept
two numbers and onClick of any of the four Buttons (+,-,*,/) would display the answer in a TextField. The
onClick Listener has to be registered to the ActivityMain class.
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Part B (to be completed by students)

(Students must submit the soft copy as per the following segments. The soft copy must be uploaded on
the Blackboard. The filename should be Batch_RollNo_Exp_No)

Roll No.: I010 Name: Deepak Chaudhary


Prog/Yr./Sem: MBATech (IT)/3rd/6th Batch: A1
Date of Experiment: Date of Submission: 18-12-21

1. Program Scenario and Program code: (Write Scenario and Paste your program code (Java,
xml resource and layout)).

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="@string/my_name"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/mycolor" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Number"
android:textSize="20sp" />

<EditText
android:id="@+id/txt_num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />

<TextView
android:layout_width="wrap_content"
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development


android:layout_height="wrap_content"
android:text="Second Number"
android:textSize="20sp" />

<EditText
android:id="@+id/txt_num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />

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

<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="+" />

<Button
android:id="@+id/btn_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="-" />

<Button
android:id="@+id/btn_mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="*" />

<Button
android:id="@+id/btn_div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="/" />
</LinearLayout>

<TextView
android:id="@+id/txt_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development


android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="Output"
android:textSize="20sp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
</LinearLayout>
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

MainActivity.java

package com.example.calculator;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {

EditText text_num1;
EditText text_num2;
Button button_add, button_sub, button_mul, button_div;
TextView text_output;

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

text_num1 = findViewById(R.id.txt_num1);
text_num2 = findViewById(R.id.txt_num2);
button_add = findViewById(R.id.btn_add);
button_sub = findViewById(R.id.btn_sub);
button_mul = findViewById(R.id.btn_mul);
button_div = findViewById(R.id.btn_div);
text_output = findViewById(R.id.txt_out);

button_add.setOnClickListener(this);
button_sub.setOnClickListener(this);
button_mul.setOnClickListener(this);
button_div.setOnClickListener(this);

@Override
public void onClick(View view) {
String num1 = text_num1.getText().toString();
String num2 = text_num2.getText().toString();
if(num1.isEmpty() || num2.isEmpty()){
Toast.makeText(MainActivity.this,"One of the number is blank",
Toast.LENGTH_SHORT).show();
}
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development


else{
switch (view.getId()){
case R.id.btn_add:
double addition = Double.parseDouble(num1) +
Double.parseDouble(num2);
text_output.setText(String.valueOf(addition));
break;
case R.id.btn_sub:
double subtration = Double.parseDouble(num1) -
Double.parseDouble(num2);
text_output.setText(String.valueOf(subtration));
break;
case R.id.btn_mul:
double multiplication = Double.parseDouble(num1) *
Double.parseDouble(num2);
text_output.setText(String.valueOf(multiplication));
break;
case R.id.btn_div:
if(num2.equals("0")){
text_output.setText("Divide by Zero Error");
}
else{
double division = Double.parseDouble(num1) /
Double.parseDouble(num2);
text_output.setText(String.valueOf(division));
}
break;
}
}
}
}
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

colors.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="mycolor">#9E36DF</color>
</resources>

strings.xml

<resources>
<string name="app_name">Calculator</string>
<string name="my_name">My Basic Calculator</string>
</resources>
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

2. Output: (Paste your program input and output screen shots).

Addition
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Subtraction
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Multiplication
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Division
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Divide by 0
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

Number field is blank


SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

3. Observations: A brief description of the design aspects and working of the code in your own
words.
In this experiment, we constructed a basic calculator in which the user must enter two values
and choose between addition, subtraction, multiplication, or division as an operation. The
result is computed using the Java programming language. A notice indicating "One input is
blank" would be displayed if one of the two inputs was missing.
Linear layout tags, text view tags, edit text tags, and button tags have all been utilised.

4. Questions:
a. Explain any five android concepts you learned after performing this practical.

Linear Layout aligns all the children in a single


direction, either vertically or
horizontally
Relative Layout displays the child views in relative
positions
Table Layout groups the views into rows and
columns
Absolute Layout Enables the user to specify the exact
location of the children
Frame Layout it is a placeholder that is used to display
a single view
List View displays the list of scrollable items

b. Draw & Explain with respect to layouts the Scene Graph of the experiment.
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

There are three sections in the above scene graph.


The first linear layout has three text displays and two text boxes for the user to enter values. My
Basic Calculator is displayed in the first text view. The first number is displayed in the second
text view, while the second number is displayed in the third text view.
To compute the results, the second linear arrangement has four buttons: addition, subtraction,
multiplication, and division. The output is displayed using Text View. Output will be displayed
by default.
SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Android App Development

5. Conclusion (Learning Outcomes): How were the outcomes defined for the experiment in
Part A fulfilled through the scenarios?
Ans. The linear layout tag was used to align all of the children in the same direction, the text
view tag was used to add text, the edit text tag was used to enter text/data from the user, and
the button tag was used to inject buttons into the app to do the desired action. The java
programming language is also utilised to compute the findings.

You might also like