Professional Documents
Culture Documents
LAB REPORT: 2
Course Title: Mobile Application Development Laboratory
Course Code: CSE-426 Section: DA
Student Details
Name ID
[For Teachers use only: Don’t Write Anything inside this box]
2. OBJECTIVES:
The objective of this lab exercise is to create a simple Android application with a user
interface that includes an EditText field, two buttons, and a ListView. The EditText field
allows the user to enter text, and the "Add" button adds the entered text to the ListView.
The "Back" button takes the user back to the previous page. The lab aims to demonstrate
basic UI components and event handling in Android development.
3. PROCEDURE:
The procedure involved in this lab report includes the following steps for xml code and
java code:
1. The layout file defines the user interface components using a LinearLayout as the root
view.
2. The EditText component (edtext) allows the user to input text.
3. Two buttons (btn1 and btn2) are included. The "Add" button adds the text from the
EditText to the ListView, and the "Back" button returns the user to the previous page.
4. The ListView component (list) displays the added items.
5. The Secondpage class extends the AppCompatActivity class, representing the activity
for the second page of the application.
6. Various variables are declared to represent the UI components, including edtext
(EditText), btn1 and btn2 (Buttons), list (ListView), list1 (ArrayList<String>), and
adapter (ArrayAdapter<String>).
7. In the Oncreate() method , the layout using setContentView
(R.layout.activity.secondpage)
8. References to the UI components are obtained using findViewById() and assigned to
their respective variables.
9. An ArrayList (list1) is initialized to store the entered text items, and an ArrayAdapter
(adapter) is created to bind the ArrayList to the ListView.
10. An onClickListener is added to the "Add" button (btn1) to handle the click event.
When the button is clicked, the text from the EditText is retrieved, added to the
ArrayList (list1), and the adapter is notified of the data change.
11. Finally, the adapter is set to the ListView to display the updated items.
4. IMPLEMENTATION:
XML CODE:
<?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=".Secondpage">
<EditText
android:id="@+id/edtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Text Here"
android:layout_margin="30dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginRight="30dp"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="Back"/>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
JAVA CODE:
package com.example.homepage;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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 java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondpage);
edtext = (EditText)findViewById(R.id.edtext);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
list = (ListView) findViewById(R.id.list);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = edtext.getText().toString();
list1.add(name);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
}
7. OUTPUT:
During testing, the implemented code successfully achieved the desired functionality. The
application launched the second page with the provided user interface components. The
"Add" button successfully added the entered text to the ListView, which dynamically
updated to display the newly added items. The "Back" button returned the user to the
previous page without any issues.
8. DISCUSSION
The implemented code demonstrates a basic Android application that incorporates user
interface components such as EditText, Buttons, and ListView. It showcases the use of
event handling and data binding through the use of onClickListener and ArrayAdapter. The
application allows users to input text, add it to a list, and view the updated list of items.