Lab Report Amit
Lab Report Amit
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):
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):
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):
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):
Code (MainActivity.java):
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):
Code (MainActivity.java):
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):
<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;
@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);
}
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
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;
@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);
return rowView;
}
}
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
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.
<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>
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;
@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);
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.
<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>
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.
<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;
@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());
}
@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:
<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>
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);
number1 = findViewById(R.id.number1);
number2 = findViewById(R.id.number2);
resultTextView = findViewById(R.id.resultTextView);
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.