Name: Aashish hooda
Roll No: 01
University Roll No:2221016
Section: G1(BCA)
Problem Statement14: Create a android application to show use of array
adapter. XML CODE
<?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">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp" /></LinearLayout>
Java Code:
package com.example.arrayadapterexample;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);
// Create a data source (ArrayList of
strings) ArrayList<String> items = new
ArrayList<>(); items.add("Apple");
items.add("Banana");
items.add("Cherry");
items.add("Date");
items.add("Elderberry");
items.add("Fig");
items.add("Grapes");
// Create an ArrayAdapter
ArrayAdapter<String> adapter = new
ArrayAdapter<>( this,
android.R.layout.simple_list_item_1,
items
);
// Set the adapter to the ListView
listView.setAdapter(adapter);
}
}
OUTPUT:
Name: Aashish hooda
Roll No:01
University Roll No:2221016
Section: G1(BCA)
Problem Statement15: Create an android application to perform CRUD
operation in database
XML CODE:
<?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/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:padding="12dp" />
<EditText
android:id="@+id/edit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID (for Update/Delete)"
android:padding="12dp"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/button_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Record"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/button_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View All Records"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/button_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update Record"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/button_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete Record"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/text_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="16sp"
android:padding="8dp"
android:background="#E0E0E0"
android:scrollbars="vertical" /></LinearLayout>
Java Code:
package com.example.crudapp;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SǪLiteDatabase;
import android.database.sqlite.SǪLiteOpenHelper;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{ private EditText nameEditText, idEditText;
private TextView resultTextView;
private DBHelper dbHelper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.edit_name);
idEditText = findViewById(R.id.edit_id);
resultTextView = findViewById(R.id.text_result);
dbHelper = new DBHelper(this);
Button addButton = findViewById(R.id.button_add);
Button viewButton = findViewById(R.id.button_view);
Button updateButton = findViewById(R.id.button_update);
Button deleteButton = findViewById(R.id.button_delete);
addButton.setOnClickListener(view -> {
String name = nameEditText.getText().toString();
if (name.isEmpty()) {
Toast.makeText(this, "Name cannot be empty", Toast.LENGTH_SHORT).show();
return;}
dbHelper.insertRecord(name);
Toast.makeText(this, "Record Added", Toast.LENGTH_SHORT).show();
}); viewButton.setOnClickListener(view -> {
String records = dbHelper.getAllRecords();
resultTextView.setText(records); });updateButton.setOnClickListener(view -> {
String id = idEditText.getText().toString();
String name = nameEditText.getText().toString();
if (id.isEmpty() || name.isEmpty()) {
Toast.makeText(this, "Both ID and Name are requiredToast.LENGTH.SHORT.
show(); return;}
dbHelper.updateRecord(Integer.parseInt(id), name);
Toast.makeText(this, "Record Updated", Toast.LENGTH_SHORT).show(); });
deleteButton.setOnClickListener(view -> {
String id = idEditText.getText().toString();
if (id.isEmpty()) {
Toast.makeText(this, "ID is required", Toast.LENGTH_SHORT).show();
return; }dbHelper.deleteRecord(Integer.parseInt(id));
Toast.makeText(this, "Record Deleted", Toast.LENGTH_SHORT).show(); }); }
static class DBHelper extends SǪLiteOpenHelper
private static final String DATABASE_NAME =
"MyDatabase.db"; private static final int DATABASE_VERSION
= 1;
private static final String TABLE_NAME = "Users";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_NAME = "Name"; DBHelper(MainActivity
context) super(context, DATABASE_NAME, null, DATABASE_VERSION);
public void onCreate(SǪLiteDatabase db) {
db.execSǪL("CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER
PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT)");}
public void onUpgrade(SǪLiteDatabase db, int oldVersion, int newVersion) {
db.execSǪL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);}
void insertRecord(String name) {
SǪLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
db.insert(TABLE_NAME, null, values);
db.close(); } String getAllRecords()
{ SǪLiteDatabase db =
this.getReadableDatabase();
Cursor cursor = db.rawǪuery("SELECT * FROM " + TABLE_NAME, null);
StringBuilder records = new StringBuilder();
while (cursor.moveToNext()) {
records.append("ID: ").append(cursor.getInt(0))
.append(", Name: ").append(cursor.getString(1)).append("\n"); }
cursor.close(); db.close();return records.toString(); } void updateRecord(int
id,
String name) {SǪLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
db.update(TABLE_NAME, values, COLUMN_ID + "=?", new
String[]{String.valueOf(id)});
db.close(); } void deleteRecord(int id)
{ SǪLiteDatabase db =
this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN_ID + "=?", new String[]{String.valueOf(id)});
db.close();}}}
OUTPUT:
Name: Aashish hooda
Roll No:01
University Roll No:2221016
Section: G1(BCA)
Problem Statement16: Create an android application to show EMEI number and
access the phone state .
XML CODE:
<?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">
<TextView
android:id="@+id/text_imei"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IMEI Number: "
android:textSize="18sp"
android:padding="8dp" />
<Button
android:id="@+id/button_fetch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fetch IMEI" /></LinearLayout>
Java Code:
package com.example.imeiapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
private static final int REǪUEST_PHONE_STATE =
100;
private TextView imeiTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imeiTextView = findViewById(R.id.text_imei);
Button fetchButton = findViewById(R.id.button_fetch);
fetchButton.setOnClickListener(view -> {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
REǪUEST_PHONE_STATE); } else {
fetchIMEI();}});} private void fetchIMEI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Ǫ) {
imeiTextView.setText("Access to IMEI is restricted on Android 10 and above.");
} else { try {
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
if (telephonyManager != null) {
String imei = telephonyManager.getDeviceId();
imeiTextView.setText("IMEI Number: " + imei); }
} catch (SecurityException e) {
imeiTextView.setText("Permission not granted.") } } }
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REǪUEST_PHONE_STATE) {
if (grantResults.length > 0 CC grantResults[0]
==PackageManager.PERMISSION_GRANTED) {
fetchIMEI();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show(); }}}}
Name: Aashish hooda
Roll No:01
University Roll No:2221016
Section: G1(BCA)
Problem Statement: . Create an android application to show EMEI number and
access the phone state .
OUTPUT:
Name: Aashish hooda
Roll No:01
University Roll No:2221016
Section: G1(BCA)
Problem Statement17: Create an application to show OPT based authentication
. XML CODE:
<?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/edit_phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone"
android:padding="12dp" />
<Button
android:id="@+id/button_send_otp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send OTP"
android:layout_marginTop="16dp" />
<EditText
android:id="@+id/edit_otp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter OTP"
android:inputType="number"
android:padding="12dp"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/button_verify_otp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Verify OTP"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/text_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="16dp"
android:textSize="16sp"
android:textColor="@android:color/holo_red_dark" /></LinearLayout>
Java Code:
package com.example.otpapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity
{ private EditText phoneNumberEditText, otpEditText;
private TextView statusTextView;
private String generatedOtp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNumberEditText = findViewById(R.id.edit_phone_number);
otpEditText = findViewById(R.id.edit_otp);
statusTextView = findViewById(R.id.text_status);
Button sendOtpButton = findViewById(R.id.button_send_otp);
Button verifyOtpButton = findViewById(R.id.button_verify_otp);
// Generate and send OTP
sendOtpButton.setOnClickListener(view -> {
String phoneNumber = phoneNumberEditText.getText().toString().trim();
if (phoneNumber.isEmpty() || phoneNumber.length() != 10) {
Toast.makeText(this, "Enter a valid 10-digit phone number",
Toast.LENGTH_SHORT).show();return; } // Simulate OTP generation
generatedOtp = generateOtp();
Toast.makeText(this, "OTP sent: " + generatedOtp, Toast.LENGTH_SHORT).show(); });
// Verify OTP
verifyOtpButton.setOnClickListener(view -> {String enteredOtp =
otpEditText.getText().toString().trim();if (enteredOtp.equals(generatedOtp)) {
statusTextView.setText("Authentication
Successful!");statusTextView.setTextColor(getResources().getColor(android.R.color.hol
o_green_dark)); } else {
statusTextView.setText("Invalid OTP. Try again.");
statusTextView.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
} }); }
private String generateOtp() {
// Generate a 4-digit random OTP
Random random = new Random();
int otp = 1000 + random.nextInt(9000);
return String.valueOf(otp); }}
OUTPUT:
Name: Aashish hooda
Roll No:01
University Roll No:2221016
Section: G1(BCA)
Problem Statement18:Create an application to implement
login. XML CODE:
<?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">
<TextView
android:id="@+id/text_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="24dp" />
<EditText
android:id="@+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:padding="12dp" />
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="12dp"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="24dp" />
<TextView
android:id="@+id/text_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="16dp"
android:textSize="16sp"
android:gravity="center" /></LinearLayout>
Java Code:
package com.example.loginapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Hardcoded credentials for demonstration
private static final String CORRECT_USERNAME = "admin";
private static final String CORRECT_PASSWORD =
"password123"; protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText usernameEditText =
findViewById(R.id.edit_username); EditText passwordEditText
= findViewById(R.id.edit_password); Button loginButton =
findViewById(R.id.button_login); TextView statusTextView =
findViewById(R.id.text_status);
loginButton.setOnClickListener(view -> {
String username =
usernameEditText.getText().toString().trim(); String password
= passwordEditText.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please fill in both fields", Toast.LENGTH_SHORT).show();
return; }
if (username.equals(CORRECT_USERNAME)
CC password.equals(CORRECT_PASSWORD)) {
statusTextView.setText("Login Successful");
statusTextView.setTextColor(getResources().getColor(android.R.color.holo_green_dark)
);
} else {
statusTextView.setText("Invalid Username or Password");
statusTextView.setTextColor(getResources().getColor(android.R.color.holo_red_dark)) }
}); }}
OUTPUT: