MB Programming
MB Programming
Pokhara
Submitted To:
Gyaneshwor Dhungana
BCA Program
Submitted By:
2025
S.N. TITLE SUBMISSION REMARK
DATE
LAB 1
OBJECTIVE: TO INSTALL AND SET UP ANDROID STUDIO
FOR ANDROID DEVELOPMENT.
TOOLS AND TECHNOLOGIES:
Android Studio
Java Development Kit (JDK)
Android SDK
Windows/macOS/Linux OS
Emulator or physical Android device
THEORY:
a) Android Studio
Android Studio is the official IDE for Android development, built on IntelliJ IDEA. It
offers comprehensive tools for building, testing, and debugging Android apps.
b) Java Development Kit (JDK)
The JDK is required to compile and run Java-based Android applications. It provides
essential tools for Java development.
c) Android SDK
The Android SDK includes APIs and developer tools needed to create, test, and debug
Android apps on various Android versions.
d) Emulator
The emulator is a virtual device that mimics real Android hardware, allowing
developers to test apps across different devices and screen sizes without a physical
phone.
PROCEDURE:
a) Download the Android Studio installer from the official Android developer website.
b) Click on the “Download Android Studio” option.
d) “Android Studio Setup” will appear on the screen and click “Next” to proceed.
e) Select the components that we want to install and click on the “Next” button.
f) Now, browse the location where we want to install the Android Studio and
click “Next” to proceed.
g) Choose a start menu folder for the “Android Studio” shortcut and click the “install”
button to proceed.
h) After the successful completion of the installation, click on the “Next” button.
Now, your Android Studio welcome screen will appear on the screen.
j) “Android Studio Setup Wizard” will appear on the screen with the welcome wizard.
Click on the “Next button”.
k) Select (check) the “Standard” option if you are a beginner and do not have any idea
about Android Studio. It will install the most common settings and options for you.
Click “Next” to proceed.
l) Now, select the user interface theme as you want. (I prefer Dark theme (Dracula) that
is most liked by the coders). Then, click on the “Next” button.
m) Now, click on the “Finish” button to download all the SDK components.
n) After downloading all the necessary components, click on the “Finish” button.
CONCLUSION:
In this lab, we set up Android Studio, including the JDK, Android SDK, and emulator, gaining a
foundational understanding of the Android development environment. While focused on
installation, it introduced key tools needed to build apps. Performance may vary across devices,
especially those with limited memory. For future steps, exploring SDK and AVD Manager,
adding plugins, customizing the IDE, and building a basic app will reinforce learning. Diving
into Kotlin will also open up modern development possibilities.
LAB 2
OBJECTIVE: TO UNDERSTAND AND EXPLORE THE
ANDROID PROJECT FOLDER STRUCTURE.
THEORY:
Understanding the Structure of an Android Project
a) Overview:
An Android project follows a structured folder hierarchy that separates source code,
resources, and configuration files. This organization helps developers efficiently manage and
navigate the project during development.
b) app/ Directory:
This is the core directory containing everything related to the application. It typically
includes three main subfolders:
manifests/ – for the app's manifest file
java/ – for source code and test files
res/ – for all non-code resources
c) manifests/ Folder:
Contains the AndroidManifest.xml file, which defines essential app details like the package
name, components (activities, services), permissions, and other configuration metadata.
d) java/ Folder:
Holds the app’s source code, written in Java or Kotlin, following the package naming
structure. This folder also includes directories for unit and instrumented tests.
e) res/ Folder:
Stores all non-code resources, organized into subfolders:
layout/ – XML files defining the UI layout
drawable/ – Images and drawable assets
values/ – XML files for strings, styles, colors, etc.
mipmap/ – Launcher icons optimized for different screen densities
PROCEDURE:
a) Open Android Studio.
c) Name the project and configure the language (Java) and SDK version.
d) Once the project is created, explore the Project view in Android Studio.
java/
res/
f) Open each folder and understand the role of the files inside.
CONCLUSION:
This lab introduced the structure of an Android project, covering key folders like app, manifests,
java, and res, and explained the role of Gradle in managing dependencies. It focused solely on
structural aspects, excluding app development and advanced topics like multi-module setup or
custom Gradle configurations. For future enhancement, we can build a simple project to observe
structural changes, explore custom Gradle settings, and include Kotlin-based development for
deeper_understanding.
LAB 3
OBJECTIVE: TO CREATE A “HELLO WORLD”
APPLICATION USING ANDROID STUDIO.
THEORY
THEORY:
Understanding the Android Project Structure
Creating an Android app involves more than just a single file—it's a well-organized set of folders
and files that work together. The key components include:
manifests/ – Contains the AndroidManifest.xml file, which acts as the app’s "identity
card." It defines essential details like the app’s name, components (e.g., activities),
permissions, and more.
java/ – This is where the core logic resides. It holds Java or Kotlin files that control how
the app functions. This includes the main activity and other supporting classes.
res/ – Short for resources, this folder contains non-code elements like layouts, strings,
images, and styles that shape the app’s appearance and user experience.
The Role of XML in UI Design
Android uses XML files to design app screens. While drag-and-drop tools exist, XML provides a
more precise and structured way to define UI elements—like buttons, text views, colors, and
layouts. These files are stored in the res/layout/ directory, making the interface design clean and
reusable.
Writing Logic with Java
After designing the UI in XML, Java brings it to life. For example, in a simple "Hello World" app,
the MainActivity.java file handles what appears on the screen and responds to user actions. It also
manages the app's lifecycle—what happens when the app launches, pauses, resumes, or exits.
PROCEDURE:
3. Set the Name as "HelloWorld", choose Java as the programming language, and set
the minimum SDK (e.g., API 21 or higher).
AndroidManifest.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:tools="http://schemas.android.com/tools">
4.
5. <application
6. android:allowBackup="true"
7. android:dataExtractionRules="@xml/data_extraction_rules"
8. android:fullBackupContent="@xml/backup_rules"
9. android:icon="@mipmap/ic_launcher"
10. android:label="@string/app_name"
11. android:roundIcon="@mipmap/ic_launcher_round"
12. android:supportsRtl="true"
13. android:theme="@style/Theme.Test"
14. tools:targetApi="31">
15. <activity
16. android:name=".MainActivity"
17. android:exported="true">
18. <intent-filter>
19. <action android:name="android.intent.action.MAIN" />
20. <category android:name="android.intent.category.LAUNCHER" />
21. </intent-filter>
22. </activity>
23. </application>
24. </manifest>
Colors.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <resources>
3. <color name="black">#FF000000</color>
4. <color name="white">#FFFFFFFF</color>
5. </resources>
strings.xml
1. <resources>
2. <string name="app_name">Test</string>
3. </resources>
activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout
3.
4. xmlns:android="http://schemas.android.com/apk/res/android"
5. xmlns:tools="http://schemas.android.com/tools"
6. android:layout_width="match_parent"
7. android:layout_height="match_parent"
8. tools:context=".MainActivity">
9. <TextView
10. android:id="@+id/helloText"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:text="Hello Guys!"
14. android:textSize="24sp"
15. android:layout_alignParentTop="true"
16. android:layout_centerHorizontal="true"
17. android:layout_marginTop="20dp" />
18. </RelativeLayout>
MainActivity.java
1. package com.example.test;
2. import android.os.Bundle;
3. import androidx.appcompat.app.AppCompatActivity;
4. public class MainActivity extends AppCompatActivity {
5. @Override
6. protected void onCreate(Bundle savedInstanceState) {
7. super.onCreate(savedInstanceState);
8. setContentView(R.layout.activity_main);
9. }
10. }
OUTPUT:
CONCLUSION:
In this lab, we built a basic "Hello World" app using Android Studio. We explored the project
structure, used XML (activity_main.xml) for UI design, Java (MainActivity.java) for logic, and
reviewed config files like AndroidManifest.xml, colors.xml, and strings.xml. The app was
simple, with no interactivity or advanced features. In the future, we can add user input, lifecycle
handling, fragments, and switch to Kotlin for improved performance and modern development.
LAB 4
OBJECTIVE: TO DESIGN A FORM USING LINEARLAYOUT
WITH BASIC VIEWS SUCH AS TEXTVIEW, EDITTEXT,
BUTTON, RADIOGROUP, ETC.
THEORY:
Android Project Structure: When building an Android app, the project is organized into
several key folders, each with a specific role:
manifests/ – Contains AndroidManifest.xml, the app’s blueprint. It defines components
like activities and tells Android how the app should run.
java/ – Holds the Java (or Kotlin) source code, including files like MainActivity.java,
which define the app’s behavior.
res/layout/ – Stores XML files that describe the user interface for each screen.
res/values/ – Contains reusable resources like strings, colors, and styles used throughout
the app.
Designing the UI with XML: In Android, UI is designed using XML. It allows you to define the
layout and appearance of elements like text, buttons, and input fields. In this lab, we used
LinearLayout to arrange items in a straight line and added views like TextView, EditText,
Button, and RadioGroup to create a simple interactive layout.
Adding Behavior with Java: After designing the layout, Java is used to make the app
interactive. For example, when a user taps a button or selects an option, Java handles the
response—such as validating input or displaying a message. This brings the static layout to life.
PROCEDURE:
3. Configure the project (e.g., Name: FormApp, Language: Java, Minimum SDK:
API 21).
4. Navigate to res/layout/activity_main.xml.
5. Replace the existing layout with a LinearLayout and add the following views:
activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:orientation="vertical"
6. android:padding="16dp">
7.
8. <TextView
9. android:text="Name:"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content" />
12.
13. <EditText
14. android:id="@+id/editTextName"
15. android:layout_width="match_parent"
16. android:layout_height="wrap_content"
17. android:hint="Enter your name" />
18.
19. <TextView
20. android:text="Email:"
21. android:layout_width="wrap_content"
22. android:layout_height="wrap_content" />
23.
24. <EditText
25. android:id="@+id/editTextEmail"
26. android:layout_width="match_parent"
27. android:layout_height="wrap_content"
28. android:hint="Enter your email"
29. android:inputType="textEmailAddress" />
30.
31. <TextView
32. android:text="Gender:"
33. android:layout_width="wrap_content"
34. android:layout_height="wrap_content" />
35.
36. <RadioGroup
37. android:id="@+id/radioGroupGender"
38. android:layout_width="wrap_content"
39. android:layout_height="wrap_content"
40. android:orientation="horizontal">
41.
42. <RadioButton
43. android:id="@+id/radioMale"
44. android:layout_width="wrap_content"
45. android:layout_height="wrap_content"
46. android:text="Male" />
47.
48. <RadioButton
49. android:id="@+id/radioFemale"
50. android:layout_width="wrap_content"
51. android:layout_height="wrap_content"
52. android:text="Female" />
53. </RadioGroup>
54.
55. <Button
56. android:id="@+id/buttonSubmit"
57. android:layout_width="wrap_content"
58. android:layout_height="wrap_content"
59. android:text="Submit" />
60. </LinearLayout>
OUTPUT:
CONCLUSION:
In this lab, we built a simple form using TextView, EditText, RadioGroup, and Button within a
LinearLayout. It demonstrated how XML defines the UI layout and Java controls the app’s
behavior. We also explored the structure of an Android project, including manifests, java,
res/layout, and res/values. However, the app lacks input validation, user feedback, and data
storage or backend integration.
LAB 5
OBJECTIVE: TO CREATE A SURVEY FORM IN
CONSTRAINT LAYOUT USING VARIOUS WIDGETS SUCH
AS CHECKBOX, RADIOBUTTON, AND SPINNER.
THEORY:
ConstraintLayout in Android
ConstraintLayout is a powerful and flexible layout manager used to design complex user
interfaces in Android without creating deep view hierarchies. It positions and sizes views by
applying constraints relative to other views or the parent layout.
Key Features:
Positions views relative to siblings or the parent layout
Uses bias to fine-tune view alignment
Supports chains to group multiple views in a linear sequence (horizontal or vertical)
c) Configure the project with name (e.g., SurveyApp), language (Java), and minimum
SDK.
f) In MainActivity.java, write logic to collect and display or process the selected values.
CONCLUSION:
This lab involved creating a survey form using ConstraintLayout with CheckBox, RadioButton,
and Spinner widgets. We learned to build flexible layouts, handle user input, and show Toast
messages in Java.
Limitations included no input validation, no data storage, and limited options in form elements.
Future improvements could add input validation, data storage, dynamic form fields, and
enhanced GUI design with better styling and interactivity.
LAB 6
OBJECTIVE: TO CREATE A BASIC ANDROID
APPLICATION THAT DEMONSTRATES THE USE OF
STRING RESOURCES AND COLOR RESOURCES IN
THE USER INTERFACE.
THEORY:
1. Android Project Structure: An Android project consists of:
A Java/Kotlin folder containing the app’s logic.
A res/ folder holding UI layouts, drawables, strings, and colors.
An AndroidManifest.xml file for app configuration.
2. Role of XML in UI Creation: XML defines the layout and views (e.g., TextView, Button),
separating the UI design from app logic. This separation improves maintainability.
3. String Resources: String values are stored in res/values/strings.xml for reuse and easy
localization.
Example: <string name="welcome_message">Welcome to My App</string>
4. Color Resources: Colors are defined in res/values/colors.xml to ensure UI consistency and
simplify_theming.
Example: <color name="custom_blue">#2196F3</color>
5. Java Code for Logic: Java binds the XML UI components using findViewById() and
implements the app logic. In this example, the app displays a string and applies a color resource.
PROCEDURE:
c) Set project name (e.g., ResourceDemoApp), choose Java as language, and minimum
SDK.
activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout
3. xmlns:android="http://schemas.android.com/apk/res/android"
4. android:layout_width="match_parent"
5. android:layout_height="match_parent"
6. android:orientation="vertical"
7. android:background="@color/white"
8. <TextView
9. android:id="@+id/tvWelcome"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="@string/welcome_message"
13. android:layout_gravity="center"
14. android:textColor="@color/custom_blue"
15. android:textSize="24sp" />
16. </LinearLayout>
strings.xml
1. <resources>
2. <string name="app_name">ResourceDemoApp</string>
3. <string name="welcome_message">Welcome !!!</string>
4. </resources>
colors.xml
<resources>
<color name="white">#FFFFFF</color>
<color name="custom_blue">#2196F3</color>
</resources>
MainActivity.java
1. package com.example.test;
2. import android.os.Bundle;
3. import androidx.appcompat.app.AppCompatActivity;
4. public class MainActivity extends AppCompatActivity {
5. @Override
6. protected void onCreate(Bundle savedInstanceState) {
7. super.onCreate(savedInstanceState);
8. setContentView(R.layout.activity_main);
9. }
10. }
OUTPUT:
CONCLUSION:
In this lab, we built a simple Android app using string and color resources for better maintenance
and localization. We designed the UI with XML and implemented logic in Java, connecting
components via findViewById(). The app showed static text without interactivity.
Future improvements could include multi-language support and dynamic theme switching based
on user or system preferences.
LAB 7
OBJECTIVE: TO ADD A CUSTOM APPLICATION ICON IN
AN EXISTING ANDROID PROJECT.
THEORY:
An Android project is organized into several folders. Application launcher icons are stored in
the **res/mipmap** directory under various subfolders (like mipmap-hdpi, mipmap-xhdpi,
etc.) to support different screen densities. The app icon is linked using the
**AndroidManifest.xml** file.
2. Launcher Icons:
Launcher icons are the icons displayed on the home screen or app drawer. To ensure
compatibility with different devices, icons are included in multiple resolutions based on
screen density (hdpi, xhdpi, xxhdpi, etc.).
Android Studio provides Image Asset Studio to easily create and manage launcher icons. It
automatically generates all required icon sizes for different densities and updates the
necessary references in the manifest.
PROCEDURE:
h) Android Studio will automatically add the icons in all mipmap- folders.
this: android:icon="@mipmap/ic_launcher"
OUTPUT:
CONCLUSION:
In this lab, I learned to create a custom app icon using Image Asset Studio in Android Studio. I
explored launcher icons, their placement in res/mipmap, and how Android handles screen
densities. The custom icon was applied via AndroidManifest.xml.
Limitation: Only static icons were used; adaptive or animated icons were not explored.
Future Improvement: Implement adaptive icons, dark mode support, and vector-based icons
for better scalability and modern UI support.
LAB 8
OBJECTIVE: TO CREATE AN ANDROID APPLICATION
THAT DEMONSTRATES PASSING DATA BETWEEN TWO
ACTIVITIES USING INTENT.
THEORY:
Java is used to manage user interactions and define the behavior of the app. Event handling
(like button clicks) is done using event listeners such as onClickListener, which also handle
the transfer of data between activities.
PROCEDURE:
c) Name the project (e.g., IntentDemoApp) and select Java as the language.
j) Run the app on an emulator or device and test the data passing.
SOURCE CODE:
activity_main.xml
1. <LinearLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:orientation="vertical"
6. android:padding="16dp">
7.
8. <EditText
9. android:id="@+id/editTextMessage"
10. android:layout_width="match_parent"
11. android:layout_height="wrap_content"
12. android:hint="Enter your message" />
13.
14. <Button
15. android:id="@+id/buttonSend"
16. android:layout_width="wrap_content"
17. android:layout_height="wrap_content"
18. android:text="Send"
19. android:layout_marginTop="20dp"/>
20.
21. <TextView
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:text="Activity First"
25. android:layout_gravity="center"
26. android:layout_marginTop="100dp"
27. android:textSize="40dp"
28. android:fontFamily="sans-serif-medium"
29. android:textColor="@color/red"/>
30.
31. </LinearLayout>
MainActivity.java
1. package com.example.test;
2.
3. import android.content.Intent;
4. import android.os.Bundle;
5. import android.widget.Button;
6. import android.widget.EditText;
7. import androidx.appcompat.app.AppCompatActivity;
8.
9. public class MainActivity extends AppCompatActivity {
10. EditText editTextMessage;
11. Button buttonSend;
12.
13. @Override
14. protected void onCreate(Bundle savedInstanceState) {
15. super.onCreate(savedInstanceState);
16. setContentView(R.layout.activity_main);
17.
18. editTextMessage = findViewById(R.id.editTextMessage);
19. buttonSend = findViewById(R.id.buttonSend);
20.
21. buttonSend.setOnClickListener(v -> {
22. String message = editTextMessage.getText().toString();
23. Intent intent = new Intent(MainActivity.this,
24. SecondActivity.class); intent.putExtra("message_key", message);
25. startActivity(intent);
26. });
27. }
28. }
activity_second.xml
1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. android:layout_width="match_parent"
3. android:layout_height="match_parent"
4. android:orientation="vertical"
5. android:padding="16dp">
6. <TextView
7. android:id="@+id/textViewResult"
8. android:layout_width="match_parent"
9. android:layout_height="wrap_content"
10. android:textSize="20sp"/>
11. <TextView
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:text="Activity Second"
15. android:layout_gravity="center"
16. android:layout_marginTop="100dp"
17. android:textSize="40dp"
18. android:textColor="@color/red"/> </LinearLayout>
SecondActivity.java
1. package com.example.test;
2. import android.os.Bundle;
3. import android.widget.TextView;
4. import androidx.appcompat.app.AppCompatActivity;
5. public class SecondActivity extends AppCompatActivity {
6. TextView textViewResult;
7. @Override
8. protected void onCreate(Bundle savedInstanceState) {
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.activity_second);
11. textViewResult = findViewById(R.id.textViewResult);
12. String receivedMessage = getIntent().getStringExtra("message_key");
13. textViewResult.setText(receivedMessage);
14. } }
OUTPUT:
CONCLUSION:
In this lab, I learned to use explicit intents to pass data between two activities in an Android app
using putExtra() and getStringExtra(). This helped us understand intent-based navigation and
basic interactivity. The Limitation of The app only supports passing simple strings; it doesn’t
handle complex data or return results. The Future Improvement is to Use
startActivityForResult() or ActivityResultLauncher for two-way communication and pass objects
via Bundle or Parcelable.
LAB 9
OBJECTIVE: TO DEMONSTRATE THE USE OF
STARTACTIVITYFORRESULT() TO GET DATA BACK
FROM A SECOND ACTIVITY.
THOERY:
h) In SecondActivity.java, get input from EditText and send it back using setResult().
activity_main.xml
1. <LinearLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:padding="16dp"
6. android:orientation="vertical">
7.
8. <Button
9. android:id="@+id/buttonOpen"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="Open Second Activity" />
13.
14. <TextView
15. android:id="@+id/textViewResult"
16. android:layout_width="wrap_content"
17. android:layout_height="wrap_content"
18. android:layout_marginTop="24dp"
19. android:textSize="18sp"
20. android:text="Result will appear here" />
21. </LinearLayout>
MainActivity.java
1. package com.example.test;
2. import android.app.Activity;
3. import android.content.Intent;
4. import android.os.Bundle;
5. import android.widget.Button;
6. import android.widget.TextView;
7. import androidx.appcompat.app.AppCompatActivity;
8.
9. public class MainActivity extends AppCompatActivity {
10. private static final int REQUEST_CODE = 1;
11. TextView textViewResult;
12. Button buttonOpen;
13. @Override
14. protected void onCreate(Bundle savedInstanceState) {
15. super.onCreate(savedInstanceState);
16. setContentView(R.layout.activity_main);
17. textViewResult = findViewById(R.id.textViewResult);
18. buttonOpen =
findViewById(R.id.buttonOpen); 19.
20. buttonOpen.setOnClickListener(v -> {
21. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
22. startActivityForResult(intent, REQUEST_CODE);
23. });
24. }
25. @Override
26. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
27. super.onActivityResult(requestCode, resultCode, data);
28. if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
29. String result = data.getStringExtra("returnedData");
30. textViewResult.setText(result);
31. }
32. }
33. }
activity_second.xml
1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. android:layout_width="match_parent"
3. android:layout_height="match_parent"
4. android:padding="16dp"
5. android:orientation="vertical">
6. <EditText
7. android:id="@+id/editTextInput"
8. android:layout_width="match_parent"
9. android:layout_height="wrap_content"
10. android:hint="Enter data to return" />
11. <Button
12. android:id="@+id/buttonReturn"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. android:layout_marginTop="20dp"
16. android:text="Return Data" />
17. </LinearLayout>
SecondActivity.java
1. package com.example.test;
2. import android.app.Activity;
3. import android.content.Intent;
4. import android.os.Bundle;
5. import android.widget.Button;
6. import android.widget.EditText;
7. import androidx.appcompat.app.AppCompatActivity;
8. public class SecondActivity extends AppCompatActivity {
9. EditText editTextInput;
10. Button buttonReturn;
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.activity_second);
15.
16. editTextInput = findViewById(R.id.editTextInput);
17. buttonReturn = findViewById(R.id.buttonReturn);
18.
19. buttonReturn.setOnClickListener(v -> {
20. String message =
21. editTextInput.getText().toString(); Intent
22. resultIntent = new Intent();
23. resultIntent.putExtra("returnedData", message);
24. setResult(Activity.RESULT_OK, resultIntent);
25. finish();
26. }
27. }
OUTPUT:
CONCLUSION:
In this lab, I learned how to use startActivityForResult() to pass data between two activities
in an Android application. We successfully created an interface where data entered in the
second activity was returned and displayed in the first activity. One limitation is that
startActivityForResult() is now deprecated in newer Android versions. In future improvements,
we can replace it with the modern ActivityResultLauncher API for better compatibility and
cleaner code.
LAB 10
OBJECTIVE: TO DEMONSTRATE THE ANDROID ACTIVITY
LIFECYCLE USING LOGCAT IN ANDROID STUDIO.
THEORY:
Lifecycle of an Activity:
From creation to destruction, an Android activity goes through multiple stages. Lifecycle
callback is used to manage these phases.
PROCEDURE:
c) Name the project (e.g., ActivityLifecycleDemo) and select Java as the language.
o onCreate()
o onStart()
o onResume()
o onPause()
o onStop()
o onRestart()
o onDestroy()
f) Inside each method, use Log.d() to print a message indicating the method execution.
h) Use Logcat to observe the printed lifecycle messages as the activity state changes
(e.g., when the app is minimized or closed).
SOURCE CODE:
activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:orientation="vertical"
6. android:gravity="center"
7. android:padding="16dp">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="Activity Lifecycle Demo"
13. android:textSize="20sp"
14. android:textStyle="bold" />
15. </LinearLayout>
MainActivity.java
1. package com.example.activitylifecycledemo;
2. import android.os.Bundle;
3. import android.util.Log;
4. import androidx.appcompat.app.AppCompatActivity;
5. public class MainActivity extends AppCompatActivity {
6. private static final String TAG = "Lifecycle";
7. @Override
8. protected void onCreate(Bundle savedInstanceState) {
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.activity_main);
11. Log.d(TAG, "onCreate called");
12. }
13. @Override
14. protected void onStart() {
15. super.onStart();
16. Log.d(TAG, "onStart called");
17. }
18.
19. @Override
20. protected void onResume() {
21. super.onResume();
22. Log.d(TAG, "onResume called");
23. }
24.
25. @Override
26. protected void onPause() {
27. super.onPause();
28. Log.d(TAG, "onPause called");
29. }
30.
31. @Override
32. protected void onStop() {
33. super.onStop();
34. Log.d(TAG, "onStop called");
35. }
36.
37. @Override
38. protected void onRestart() {
39. super.onRestart();
40. Log.d(TAG, "onRestart called");
41. }
42.
43. @Override
44. protected void onDestroy() {
45. super.onDestroy();
46. Log.d(TAG, "onDestroy called");
47. }
48. }
OUTPUT:
CONCLUSION:
In this lab, I investigated the full lifecycle of an Android activity and learned how different
methods are activated at different app states. We were able to see the order of lifecycle
method calls as the activity begins, continues, pauses, and ends by using Logcat. The fact that
this project only logs one activity is one of its limitations; future research can examine multi-
activity transitions. Adding user interaction buttons to more interactively trigger various
lifecycle behaviors would be one potential improvement.