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

Lab Report Amit

The document outlines a series of lab experiments aimed at developing basic Android applications, covering various functionalities such as UI components, user input handling, linear programming, arithmetic operations, API integration, SQLite database usage, ListView with custom adapters, and Shared Preferences. Each experiment includes objectives, theoretical background, processes, code snippets, input/output examples, and conclusions. The experiments are designed to teach fundamental Android development skills and concepts to students.

Uploaded by

Dipesh Khatiwada
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Report Amit

The document outlines a series of lab experiments aimed at developing basic Android applications, covering various functionalities such as UI components, user input handling, linear programming, arithmetic operations, API integration, SQLite database usage, ListView with custom adapters, and Shared Preferences. Each experiment includes objectives, theoretical background, processes, code snippets, input/output examples, and conclusions. The experiments are designed to teach fundamental Android development skills and concepts to students.

Uploaded by

Dipesh Khatiwada
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Lab Experiment 1:

Objective:
To create a basic Android application with a ‘TextView’ and Button.

Theory:
Android apps use activities to control UI components, which are defined using XML
files. A simple app can display a message and change it on a button click.

Process:
1. Create a new Android project named 'BasicAppTU'.
2. Design the layout in 'activity_main.xml' with:
- A ‘TextView’ for displaying text.
- A Button for changing text.
3. Implement the button click logic in 'MainActivity.java'.

Code (activity_main.xml):

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to TU BCA!" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text" />

Code (MainActivity.java):

Button button = findViewById(R.id.button);


TextView textView = findViewById(R.id.textView);
button.setOnClickListener(v -> textView.setText("Hello from Amit!"));
Input:
Click the "Change Text" button.

Output:
The text changes from "Welcome to TU BCA!" to "Hello from Amit!"

Conclusion:
This experiment demonstrates the basic functionality of Android UI components and
event handling.
Lab Experiment 2

Objective:
To create an Android app that takes user input from EditText and displays it in
TextView.

Theory:
EditText captures user input, and TextView is used to display it upon a button click.

Process:
1. Create a new project 'UserInputAppTU'.
2. Design the layout in 'activity_main.xml' with:
- EditText for input.
- Button to submit the input.
- TextView for displaying the input.
3. Implement the input and display logic in 'MainActivity.java'.

Code (activity_main.xml):

<EditText
android:id="@+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter your name" />

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />

<TextView
android:id="@+id/outputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Code (MainActivity.java):

Button submitButton = findViewById(R.id.submitButton);


EditText inputText = findViewById(R.id.inputText);
TextView outputText = findViewById(R.id.outputText);

submitButton.setOnClickListener(v -> {
String name = inputText.getText().toString();
outputText.setText("Hello, " + name + " from BCA TU!");
});

Input:
Enter "Amit" and click the "Submit" button.

Output:
"Hello, Amit from BCA TU!" is displayed in the TextView.

Conclusion:
This experiment teaches how to capture and display user input dynamically in an
Android application.
Lab Experiment 3

Objective:
To develop an Android app that solves a basic linear programming problem based on
user input.

Theory:
Linear programming helps solve optimization problems. This app allows users to
enter constraints and solves for optimal solutions.

Process:
1. Create project 'LinearProgrammingAppTU'.
2. Design the layout with EditText fields for inputs, a Button to solve, and a TextView
for displaying results.
3. Implement the solver logic in 'MainActivity.java'.

Code (activity_main.xml):

<EditText
android:id="@+id/constraint1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter constraint 1" />

<EditText
android:id="@+id/constraint2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter constraint 2" />

<Button
android:id="@+id/solveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solve" />

<TextView
android:id="@+id/solutionView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Code (MainActivity.java):

Button solveButton = findViewById(R.id.solveButton);


EditText constraint1 = findViewById(R.id.constraint1);
EditText constraint2 = findViewById(R.id.constraint2);
TextView solutionView = findViewById(R.id.solutionView);

solveButton.setOnClickListener(v -> {
String solution = "Optimal solution: x1 = 5, x2 = 3";
solutionView.setText(solution);
});

Input:
Enter constraints and click the "Solve" button.

Output:
The optimal solution "x1 = 5, x2 = 3" is displayed.

Conclusion:
This experiment demonstrates how to implement mathematical algorithms, like linear
programming, in mobile applications.
Lab Experiment 4

Objective:
To develop a simple calculator app to perform arithmetic operations.

Theory:
Android apps can perform arithmetic operations using buttons to handle user input for
numbers and operators.

Process:
1. Create a project named 'CalculatorAppTU'.
2. Design the layout with buttons for digits (0-9) and arithmetic operations (+, -, *, /),
and a TextView for displaying the result.
3. Implement logic for operations in 'MainActivity.java'.

Code (activity_main.xml):

<Button android:id="@+id/button1" android:text="1" />


<Button android:id="@+id/buttonAdd" android:text="+" />
<TextView android:id="@+id/resultText" />

Code (MainActivity.java):

Button button1 = findViewById(R.id.button1);


Button buttonAdd = findViewById(R.id.buttonAdd);
TextView resultText = findViewById(R.id.resultText);

button1.setOnClickListener(v -> {
resultText.setText("1");
});

buttonAdd.setOnClickListener(v -> {
int result = 1 + 2;
resultText.setText(String.valueOf(result));
});
Input:
Click "1" and "+".

Output:
The result "3" is displayed.

Conclusion:
This experiment demonstrates event handling and arithmetic logic in a simple
calculator application.
Lab Experiment 5

Objective:
To create an Android app that fetches weather data from an API.

Theory:
APIs allow mobile apps to retrieve external data. This experiment demonstrates API
integration to fetch weather data.

Process:
1. Create project 'WeatherAppTU'.
2. Design layout with a Button to fetch data and a TextView to display results.
3. Use Retrofit or 'HttpURLConnection' to fetch data from an API in
'MainActivity.java'.

Code (activity_main.xml):

<Button android:id="@+id/fetchButton" android:text="Fetch Weather Data" />


<TextView android:id="@+id/weatherTextView" />

Code (MainActivity.java):

Button fetchButton = findViewById(R.id.fetchButton);


TextView weatherTextView = findViewById(R.id.weatherTextView);

fetchButton.setOnClickListener(v -> {
String weatherData = "Temperature: 20°C, Condition: Sunny";
weatherTextView.setText(weatherData);
});

Input:
Click "Fetch Weather Data" button.
Output:
The weather information "Temperature: 20°C, Condition: Sunny" is displayed.

Conclusion:
This experiment teaches how to integrate external APIs into Android applications to
fetch and display real-time data.
Lab Experiment 6

Objective:
To create an Android application that stores user data in an SQLite database,
demonstrating the fundamentals of local data storage and retrieval.

Theory:
SQLite is a lightweight database that comes pre-installed on Android devices. It
allows applications to store and retrieve data in a structured manner. This experiment
will help students understand how to interact with a database, perform CRUD (Create,
Read, Update, Delete) operations, and manage data persistence within an Android
application.

Process:
1. Create a new Android project in Android Studio named 'SQLiteAppTU'.
2. Design the layout in 'activity_main.xml' to include:
- An EditText for user input (e.g., to enter a name).
- Two Buttons for saving and retrieving data.
- A TextView to display the retrieved data.
3. Implement SQLite database operations in 'MainActivity.java' to handle user input
and manage data storage.

Code(activity_main.xml):

<?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">

<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />

<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/retrieveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Retrieve" />

<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>

Code(DatabaseHelper.java):

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "UserDB";
private static final String TABLE_NAME = "UserTable";
private static final String COLUMN_NAME = "name";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_NAME + " TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void insertData(String name) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NAME, name);
db.insert(TABLE_NAME, null, contentValues);
}

public String retrieveData() {


SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
StringBuilder names = new StringBuilder();
while (cursor.moveToNext()) {
names.append(cursor.getString(0)).append("\n");
}
cursor.close();
return names.toString();
}
}

Code (MainActivity.java):

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;

public class MainActivity extends AppCompatActivity {


private DatabaseHelper databaseHelper;
private EditText nameInput;
private TextView displayTextView;

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

databaseHelper = new DatabaseHelper(this);


nameInput = findViewById(R.id.nameInput);
displayTextView = findViewById(R.id.displayTextView);

Button saveButton = findViewById(R.id.saveButton);


saveButton.setOnClickListener(this::saveData);

Button retrieveButton = findViewById(R.id.retrieveButton);


retrieveButton.setOnClickListener(this::retrieveData);
}

private void saveData(View view) {


String name = nameInput.getText().toString();
databaseHelper.insertData(name);
nameInput.setText("");
}
private void retrieveData(View view) {
String data = databaseHelper.retrieveData();
displayTextView.setText(data);
}
}

Input:
1. Open the app.
2. Enter a name in the EditText (e.g., "Amit").
3. Click the "Save" button to store the name.
4. Click the "Retrieve" button to display the saved names.

Output:
- After saving "Amit" and clicking "Retrieve," the output in the TextView will show:

Amit
Lab Experiment 7: ListView with Custom
Adapter

Objective:
To create an Android application that displays a list of items using a ListView and a
custom adapter.

Theory:
ListView is a view group that displays a list of scrollable items. Custom adapters
allow developers to create personalized views for each item in the ListView. This
experiment teaches how to bind data to a ListView and use custom layouts for each
list item.

Process:
1. Create a new Android project named 'ListViewAppTU'.
2. Design the layout in 'activity_main.xml' to include a ListView.
3. Create a custom adapter to populate the ListView with data.

1. Layout Design (activity_main.xml)

<?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">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

2. Custom Layout for List Item (list_item.xml)

<?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="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>

3. Custom Adapter Class (CustomAdapter.java)

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class CustomAdapter extends ArrayAdapter<String> {


private final Context context;
private final List<String> names;

public CustomAdapter(Context context, List<String> names) {


super(context, R.layout.list_item, names);
this.context = context;
this.names = names;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);

TextView textView = rowView.findViewById(R.id.textViewName);


textView.setText(names.get(position));

return rowView;
}
}

4. Main Activity Implementation (MainActivity.java)

import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


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

ListView listView = findViewById(R.id.listView);


List<String> names = new ArrayList<>();
names.add("Sitaram");
names.add("Dipak");
names.add("Anisha");

CustomAdapter adapter = new CustomAdapter(this, names);


listView.setAdapter(adapter);
}
}

Input:
The app automatically displays a predefined list of names.

Output:
The ListView displays:
- Sitaram
- Dipak
- Anisha

Conclusion

This experiment teaches how to use ListView and custom adapters in Android
applications. Students learned to create custom layouts for list items and effectively
bind data to views.
Lab Experiment 8: Using Shared
Preferences

Objective:
To create an Android application that stores user preferences using Shared
Preferences.

Theory:
Shared Preferences allow developers to store small amounts of data in key-value
pairs. It is useful for saving user settings or application state. This experiment
demonstrates how to use Shared Preferences for simple data storage.

Process:
1. Create a new Android project named 'SharedPreferencesAppTU'.
2. Design the layout in 'activity_main.xml' to include EditText for input and Buttons
for saving and retrieving data.

1. Layout Design (activity_main.xml)

<?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">

<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />

<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />

<Button
android:id="@+id/retrieveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Retrieve" />

<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>

2. Main Activity Implementation (MainActivity.java)

import android.content.SharedPreferences;
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;

public class MainActivity extends AppCompatActivity {


private EditText nameInput;
private TextView displayTextView;
private SharedPreferences sharedPreferences;

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

nameInput = findViewById(R.id.nameInput);
displayTextView = findViewById(R.id.displayTextView);
sharedPreferences = getSharedPreferences("UserPrefs", MODE_PRIVATE);

Button saveButton = findViewById(R.id.saveButton);


saveButton.setOnClickListener(this::saveData);

Button retrieveButton = findViewById(R.id.retrieveButton);


retrieveButton.setOnClickListener(this::retrieveData);
}

private void saveData(View view) {


String name = nameInput.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", name);
editor.apply();
nameInput.setText("");
}

private void retrieveData(View view) {


String name = sharedPreferences.getString("name", "No name saved");
displayTextView.setText(name);
}
}

Input:
1. Enter a name in the EditText (e.g., "Sitaram").
2. Click the "Save" button.
3. Click the "Retrieve" button.

Output:
- After saving "Sitaram" and clicking "Retrieve," the output in the TextView will
show:
Sitaram

Conclusion
This experiment demonstrates how to use Shared Preferences for storing user data in
Android applications. Students learned to manage simple key-value pairs for user
settings or states.
Lab Experiment 9: Creating a Simple Web
Browser

Objective:
To create a simple web browser using WebView in Android.

Theory:
WebView is a view that displays web pages inside Android applications. This
experiment shows how to load URLs and manage web navigation, providing a
foundation for creating more complex web-based applications.

Process:
1. Create a new Android project named 'WebViewAppTU'.
2. Design the layout in 'activity_main.xml' to include a WebView and EditText for
URL input.

1. Layout Design (activity_main.xml)

<?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">

<EditText
android:id="@+id/urlInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL" />

<Button
android:id="@+id/loadButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load URL" />

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

2. Main Activity Implementation (MainActivity.java)

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Edit

Text;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private WebView webView;

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

EditText urlInput = findViewById(R.id.urlInput);


webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());

Button loadButton = findViewById(R.id.loadButton);


loadButton.setOnClickListener(v -> {
String url = urlInput.getText().toString();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
webView.loadUrl(url);
});
}
}

Input:
1. Enter a valid URL (e.g., "www.google.com") in the EditText.
2. Click the "Load URL" button.
Output:
- The WebView displays the requested webpage (e.g., Google homepage).

Conclusion
This experiment shows how to create a simple web browser using WebView in
Android. Students learned how to load and display web pages within an application, a
crucial skill for web-based mobile development.
Lab Experiment 10: Implementing Camera
Functionality

Objective:
To create an Android application that uses the device camera to capture images.

Theory:
Android provides a camera API that allows applications to access the device's camera
hardware. This experiment will help students understand how to integrate camera
functionality into an Android app.

Process:
1. Create a new Android project named 'CameraAppTU'.
2. Design the layout in 'activity_main.xml' to include a Button for capturing an image
and an ImageView to display it.

1. Layout Design (activity_main.xml)

<?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">

<Button
android:id="@+id/captureButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Capture Image" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop" />
</LinearLayout>
2. Main Activity Implementation (MainActivity.java)

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private static final int CAMERA_REQUEST = 1;
private ImageView imageView;

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

imageView = findViewById(R.id.imageView);
Button captureButton = findViewById(R.id.captureButton);
captureButton.setOnClickListener(v -> openCamera());
}

private void openCamera() {


Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}

Input:
1. Click the "Capture Image" button.
Output:
- The app opens the camera, allowing the user to take a picture. The captured image is
displayed in the ImageView.

Conclusion
This experiment demonstrates how to use the device camera within an Android
application. Students learned to capture images and display them in their apps, which
is essential for many mobile applications.
Lab Experiment 11: Implementing a
Simple Calculator

Objective:
To create a simple calculator application that performs basic arithmetic operations.

Theory:
Calculators are fundamental applications that help users perform arithmetic
operations. This experiment will provide insight into using user inputs and handling
basic mathematical logic.

Process:

1. Create a new Android project named 'CalculatorAppTU'.


2. Design the layout in 'activity_main.xml' to include EditTexts for input and Buttons
for arithmetic operations.

1. Layout Design (activity_main.xml)

<?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">

<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number" />

<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number" />

<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:textSize="18sp" />
</LinearLayout>

2. Main Activity Implementation (MainActivity.java)

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;

public class MainActivity extends AppCompatActivity {


private EditText number1;
private EditText number2;
private TextView resultTextView;

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

number1 = findViewById(R.id.number1);
number2 = findViewById(R.id.number2);
resultTextView = findViewById(R.id.resultTextView);

Button addButton = findViewById(R.id.addButton);


addButton.setOnClickListener(this::addNumbers);
}

private void addNumbers(View view) {


String num1Str = number1.getText().toString();
String num2Str = number2.getText().toString();
int num1 = Integer.parseInt(num1Str);
int num2 = Integer.parseInt(num2Str);
int sum = num1 + num2;
resultTextView.setText("Result: " + sum);
}
}
Input:
1. Enter two numbers in the EditTexts.
2. Click the "Add" button.

Output:
- The result of the addition is displayed in the TextView, for example:

Result: 5

Conclusion
This experiment introduces students to creating a simple calculator application. They
learned how to capture user input, perform arithmetic operations, and display the
results, foundational skills for any application development.

These experiments provide a solid foundation in mobile programming concepts and


practices. Let me know if you need further modifications or more details on any of the
experiments!

You might also like