0% found this document useful (0 votes)
46 views16 pages

Android App for ArrayAdapter and CRUD

Uploaded by

Ashish Hooda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views16 pages

Android App for ArrayAdapter and CRUD

Uploaded by

Ashish Hooda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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="[Link]

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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
ListView listView = findViewById([Link].list_view);
// Create a data source (ArrayList of
strings) ArrayList<String> items = new
ArrayList<>(); [Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Date");
[Link]("Elderberry");
[Link]("Fig");
[Link]("Grapes");

// Create an ArrayAdapter
ArrayAdapter<String> adapter = new
ArrayAdapter<>( this,
[Link].simple_list_item_1,
items
);

// Set the adapter to the ListView


[Link](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="[Link]
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 [Link];
import [Link];
import [Link];
import [Link].SǪLiteDatabase;
import [Link].SǪLiteOpenHelper;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{ private EditText nameEditText, idEditText;
private TextView resultTextView;
private DBHelper dbHelper;
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
nameEditText = findViewById([Link].edit_name);
idEditText = findViewById([Link].edit_id);
resultTextView = findViewById([Link].text_result);
dbHelper = new DBHelper(this);
Button addButton = findViewById([Link].button_add);
Button viewButton = findViewById([Link].button_view);
Button updateButton = findViewById([Link].button_update);
Button deleteButton = findViewById([Link].button_delete);
[Link](view -> {
String name = [Link]().toString();
if ([Link]()) {
[Link](this, "Name cannot be empty", Toast.LENGTH_SHORT).show();
return;}
[Link](name);
[Link](this, "Record Added", Toast.LENGTH_SHORT).show();
}); [Link](view -> {
String records = [Link]();
[Link](records); });[Link](view -> {
String id = [Link]().toString();
String name = [Link]().toString();
if ([Link]() || [Link]()) {
[Link](this, "Both ID and Name are [Link].

show(); return;}
[Link]([Link](id), name);
[Link](this, "Record Updated", Toast.LENGTH_SHORT).show(); });
[Link](view -> {
String id = [Link]().toString();
if ([Link]()) {
[Link](this, "ID is required", Toast.LENGTH_SHORT).show();
return; }[Link]([Link](id));
[Link](this, "Record Deleted", Toast.LENGTH_SHORT).show(); }); }

static class DBHelper extends SǪLiteOpenHelper


private static final String DATABASE_NAME =
"[Link]"; 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) {
[Link]ǪL("CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER
PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT)");}
public void onUpgrade(SǪLiteDatabase db, int oldVersion, int newVersion) {
[Link]ǪL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);}
void insertRecord(String name) {
SǪLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](COLUMN_NAME, name);
[Link](TABLE_NAME, null, values);
[Link](); } String getAllRecords()
{ SǪLiteDatabase db =
[Link]();
Cursor cursor = [Link]Ǫuery("SELECT * FROM " + TABLE_NAME, null);
StringBuilder records = new StringBuilder();
while ([Link]()) {
[Link]("ID: ").append([Link](0))
.append(", Name: ").append([Link](1)).append("\n"); }
[Link](); [Link]();return [Link](); } void updateRecord(int
id,
String name) {SǪLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](COLUMN_NAME, name);
[Link](TABLE_NAME, values, COLUMN_ID + "=?", new
String[]{[Link](id)});
[Link](); } void deleteRecord(int id)
{ SǪLiteDatabase db =
[Link]();
[Link](TABLE_NAME, COLUMN_ID + "=?", new String[]{[Link](id)});
[Link]();}}}

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="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private static final int REǪUEST_PHONE_STATE =
100;
private TextView imeiTextView;
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
imeiTextView = findViewById([Link].text_imei);
Button fetchButton = findViewById([Link].button_fetch);
[Link](view -> {
if ([Link](this,
[Link].READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
[Link](this,
new String[]{[Link].READ_PHONE_STATE},
REǪUEST_PHONE_STATE); } else {
fetchIMEI();}});} private void fetchIMEI() {
if ([Link].SDK_INT >= Build.VERSION_CODES.Ǫ) {
[Link]("Access to IMEI is restricted on Android 10 and above.");
} else { try {
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
if (telephonyManager != null) {
String imei = [Link]();
[Link]("IMEI Number: " + imei); }
} catch (SecurityException e) {
[Link]("Permission not granted.") } } }
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
[Link](requestCode, permissions, grantResults);
if (requestCode == REǪUEST_PHONE_STATE) {
if ([Link] > 0 CC grantResults[0]
==PackageManager.PERMISSION_GRANTED) {
fetchIMEI();
} else {
[Link](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="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{ private EditText phoneNumberEditText, otpEditText;
private TextView statusTextView;
private String generatedOtp;
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

phoneNumberEditText = findViewById([Link].edit_phone_number);
otpEditText = findViewById([Link].edit_otp);
statusTextView = findViewById([Link].text_status);
Button sendOtpButton = findViewById([Link].button_send_otp);
Button verifyOtpButton = findViewById([Link].button_verify_otp);
// Generate and send OTP
[Link](view -> {
String phoneNumber = [Link]().toString().trim();
if ([Link]() || [Link]() != 10) {
[Link](this, "Enter a valid 10-digit phone number",
Toast.LENGTH_SHORT).show();return; } // Simulate OTP generation
generatedOtp = generateOtp();
[Link](this, "OTP sent: " + generatedOtp, Toast.LENGTH_SHORT).show(); });
// Verify OTP
[Link](view -> {String enteredOtp =
[Link]().toString().trim();if ([Link](generatedOtp)) {
[Link]("Authentication
Successful!");[Link](getResources().getColor([Link]
o_green_dark)); } else {
[Link]("Invalid OTP. Try again.");

[Link](getResources().getColor([Link].holo_red_dark));
} }); }
private String generateOtp() {
// Generate a 4-digit random OTP
Random random = new Random();
int otp = 1000 + [Link](9000);
return [Link](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="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText usernameEditText =
findViewById([Link].edit_username); EditText passwordEditText
= findViewById([Link].edit_password); Button loginButton =
findViewById([Link].button_login); TextView statusTextView =
findViewById([Link].text_status);
[Link](view -> {
String username =
[Link]().toString().trim(); String password
= [Link]().toString().trim();
if ([Link]() || [Link]()) {
[Link](this, "Please fill in both fields", Toast.LENGTH_SHORT).show();
return; }
if ([Link](CORRECT_USERNAME)
CC [Link](CORRECT_PASSWORD)) {
[Link]("Login Successful");

[Link](getResources().getColor([Link].holo_green_dark)
);
} else {
[Link]("Invalid Username or Password");

[Link](getResources().getColor([Link].holo_red_dark)) }
}); }}

OUTPUT:

You might also like