Lab 2
Lab 2
Lab Overview:
In this lab session, students will build a simple Counter App using Java in Android Studio. The
app will have a button, and every time the button is clicked, the counter will increment by one,
and the updated count will be displayed on the screen. This exercise is designed for beginners
and assumes that students have already created a "Hello World" app.
Objective:
Step-by-Step Instructions:
1. Open activity_main.xml:
o Replace the default content with the following XML code. This creates a layout
with a TextView to display the counter and a Button to increment it.
1|Page
android:text="Counter: 0"
android:textSize="30sp"
android:layout_marginBottom="20dp"/>
Explanation:
Open the MainActivity.java file. This is where the logic of your app will be
implemented.
1. Open MainActivity.java and add the following code inside the onCreate() method:
package com.example.counterapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
2|Page
// Set an OnClickListener on the button to respond to user clicks
buttonIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Increment the counter variable
counter++;
Explanation:
Counter Variable: The integer counter keeps track of how many times the button has
been clicked.
TextView and Button: We use findViewById() to get references to the TextView and
Button defined in the layout XML file.
OnClickListener: We attach an OnClickListener to the button, which is triggered
whenever the button is clicked. Inside the listener, the counter is incremented, and the
TextView is updated with the new value.
3|Page
Summary of Concepts Covered:
1. UI Components:
o TextView: Used to display text (the counter value).
o Button: Used to allow user interaction (incrementing the counter).
2. Event Handling:
o OnClickListener: Responds to button clicks and updates the counter value.
3. Java Logic:
o Variables: We used an int variable (counter) to store the current count.
o Updating the UI: We dynamically changed the TextView text using setText()
to show the updated counter.
Further Exploration:
This lab provides a simple yet practical introduction to handling user input and updating the UI
dynamically in Android apps using Java.
4|Page
2. Android Programming Lab Session (Java): Building a Simple Calculator App
Lab Overview:
In this lab, students will create a basic Calculator App using Java in Android Studio. The app
will allow users to perform simple arithmetic operations (addition, subtraction, multiplication,
and division) between two numbers. This exercise is designed for beginners and builds upon
their knowledge from the "Hello World" and Counter app projects.
Objective:
Step-by-Step Instructions:
1. Open activity_main.xml and replace the default content with the following code:
5|Page
android:hint="Enter First Number"
android:inputType="numberDecimal"/>
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_marginStart="10dp"/>
</LinearLayout>
6|Page
</LinearLayout>
Explanation:
EditText: Used to take input from the user. The inputType="numberDecimal" ensures
that only numbers can be entered.
Button: Four buttons (Add, Subtract, Multiply, Divide) to perform arithmetic
operations.
TextView: Displays the result after the user presses one of the operation buttons.
1. Open MainActivity.java and add the following code to handle the calculator logic:
package com.example.calculatorapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
7|Page
double result = number1 + number2;
Explanation:
8|Page
EditText Parsing: We use Double.parseDouble() to convert the text input from
EditText fields into double values for calculation.
Button Click Events: Each button has an OnClickListener that defines the action when
clicked. For example, the Add button adds two numbers and updates the TextView to
show the result.
Division Error Handling: The division operation includes a check to prevent division by
zero, which would otherwise cause a runtime error.
1. UI Components:
o EditText: To take numerical input from the user.
o Button: To trigger arithmetic operations.
o TextView: To display the result.
2. Event Handling:
o We used OnClickListener to handle user interactions (button clicks).
3. User Input & Output:
o EditText values were converted to double for mathematical calculations, and the
result was displayed dynamically in the TextView.
4. Error Handling:
o We demonstrated basic error handling by preventing
9|Page
3. Android Programming Lab Session (Java): Extending the Calculator with
Memory Functions
Lab Overview:
In this lab session, students will extend their existing calculator app by adding memory
functions (M+, M-, MC, MR). These memory functions will allow users to store and recall a
value in memory, clear the memory, and add/subtract the current result to/from memory. This
exercise is designed as a follow-up to the basic calculator app (example 2).
Objective:
Step-by-Step Instructions:
Open the activity_main.xml file and update the UI to include memory function
buttons (M+, M-, MC, MR). Below the arithmetic operation buttons, we will add a row
of memory function buttons.
10 | P a g e
android:hint="Enter Second Number"
android:inputType="numberDecimal"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_marginStart="10dp"/>
</LinearLayout>
<Button
android:id="@+id/buttonMC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MC"/>
<Button
android:id="@+id/buttonMR"
11 | P a g e
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MR"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M+"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M-"
android:layout_marginStart="10dp"/>
</LinearLayout>
</LinearLayout>
Explanation:
In addition to the original buttons, we’ve added a new row of buttons for memory
functions (M+, M-, MC, MR):
o M+: Adds the current result to the memory.
o M-: Subtracts the current result from the memory.
o MC: Clears the memory.
o MR: Recalls the memory value and displays it as the result.
package com.example.calculatorapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
12 | P a g e
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
13 | P a g e
public void onClick(View v) {
textViewResult.setText("Result: " + memoryValue); // Recall
and display memory value
}
});
Explanation:
14 | P a g e
2. Build the Project:
o Click on Build > Make Project or use the shortcut Ctrl + F9.
3. Run the App:
o Click on the Run button or use the shortcut Shift + F10.
4. Test the Memory Functions:
o Enter two numbers and perform basic arithmetic operations.
o Use the memory buttons (M+, M-, MC, MR) to store, recall, and manipulate the
memory value.
Expected Output:
The app should allow users to perform standard arithmetic calculations as before.
In addition, users should be able to:
o Store a value in memory with M+ and M-.
o Recall the stored value with MR.
o Clear the memory with MC.
Conclusion:
In this lab exercise, you have extended the calculator app with useful memory functions. This
introduces the concept of global variables, allows you to manage state between calculations, and
provides more interactive functionality to the calculator app.
15 | P a g e
4. Android Programming Lab Session (Java): Adding a History Feature to the
Calculator App
Lab Overview:
In this lab session, students will extend the existing calculator app by adding a history feature.
This feature will keep track of the user's past calculations and display them in a list format. The
calculations will be displayed in a new activity, allowing students to practice using multiple
activities, passing data between activities, and displaying lists in Android.
Objective:
Step-by-Step Instructions:
1. In the activity_main.xml file, add a new button to view the calculation history. Below
the memory function buttons, add a button labeled "View History."
16 | P a g e
<!-- Buttons for basic arithmetic operations -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp">
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_marginStart="10dp"/>
</LinearLayout>
<Button
android:id="@+id/buttonMC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MC"/>
<Button
android:id="@+id/buttonMR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MR"
17 | P a g e
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M+"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/buttonMMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M-"
android:layout_marginStart="10dp"/>
</LinearLayout>
</LinearLayout>
Explanation:
We've added a "View History" button to the bottom of the layout. When clicked, it will
open a new screen (activity) showing a list of previous calculations.
18 | P a g e
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
</LinearLayout>
Explanation:
The ListView will be used to display a list of past calculations stored during the app's
use.
package com.example.calculatorapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Link UI components
EditText editTextNumber1 = findViewById(R.id.editTextNumber1);
EditText editTextNumber2 = findViewById(R.id.editTextNumber2);
Button buttonAdd = findViewById(R.id.buttonAdd);
Button buttonHistory = findViewById(R.id.buttonHistory);
19 | P a g e
TextView textViewResult = findViewById(R.id.textViewResult);
Explanation:
package com.example.calculatorapp;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
20 | P a g e
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Explanation:
Expected Output:
Conclusion:
21 | P a g e
In this lab, you've extended the calculator app by adding a history feature that displays past
calculations. You learned how to create multiple activities, pass data between them using
Intents, and display lists with a ListView. This exercise introduces important concepts like state
management and data persistence, which are essential in Android development.
22 | P a g e
5. Android Programming Lab Session (Java): Creating a Simple To-Do List
App
Lab Overview:
In this lab session, students will create a simple To-Do List app. This exercise will teach
students how to handle user input, manage a dynamic list of items, and work with ListView to
display data. The focus is on developing skills in managing user interaction, storing items in a
list, and using ArrayAdapter to populate the list.
Objective:
Step-by-Step Instructions:
23 | P a g e
android:layout_gravity="center_horizontal"/>
</LinearLayout>
Explanation:
package com.example.todolistapp;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Link UI components
EditText editTextTask = findViewById(R.id.editTextTask);
Button buttonAddTask = findViewById(R.id.buttonAddTask);
ListView listViewTasks = findViewById(R.id.listViewTasks);
24 | P a g e
taskList = new ArrayList<>();
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, taskList);
listViewTasks.setAdapter(adapter);
Explanation:
1. Build the app: Click on the Build menu and select Make Project or press Ctrl + F9.
2. Run the app: Click the Run button or press Shift + F10 to launch the app on an
emulator or physical device.
Expected Output:
25 | P a g e
Conclusion:
In this lab, you created a simple To-Do List app that allows users to add tasks to a list and view
them dynamically. This exercise introduced key concepts such as handling user input with
EditText, updating UI components with ArrayAdapter, and dynamically managing a list of
items.
26 | P a g e