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

Android-PB 2 - System

The document describes an Android application that demonstrates performing CRUD operations on a SQLite database table called Student. It includes the layout, main activity class, and database class code to add, view, update and delete records from the Student table. The table has fields for ID, name, and age.

Uploaded by

Nikhil Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Android-PB 2 - System

The document describes an Android application that demonstrates performing CRUD operations on a SQLite database table called Student. It includes the layout, main activity class, and database class code to add, view, update and delete records from the Student table. The table has fields for ID, name, and age.

Uploaded by

Nikhil Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT

Author: SYSTEM Enrollment No: 210202010_ _ _ _


Date: DD-MM-YYYY
1. Write an Android SQLite Application to demonstrate Create Read Update Delete
Operations on following table. Database Name : College , Table Name : Student
Sr. No. Field name Field Type
1 ID INTEGER + PK (Autoincrement)
2 NAME TEXT
3 AGE
INTEGER

• activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">

<EditText android:id="@+id/editId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID"
tools:layout_editor_absoluteY="16dp"
tools:ignore="Autofill,MissingConstraints" />

<EditText android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp" android:hint="Name"
app:layout_constraintTop_toTopOf="parent" />

<EditText android:id="@+id/editAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
app:layout_constraintTop_toBottomOf="@+id/editName" />

<Button android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:text="Add
Student"
app:layout_constraintTop_toBottomOf="@+id/editAge"
tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="20dp"
/>

<Button android:id="@+id/btndelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:text="Delete Student"
app:layout_constraintTop_toBottomOf="@+id/btnAdd"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="198dp" />

1|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
<Button android:id="@+id/btnupdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Update Student"
app:layout_constraintTop_toBottomOf="@+id/btndelete"
tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="16dp"
/>

<Button android:id="@+id/btnview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="View Students"
app:layout_constraintTop_toBottomOf="@+id/btnupdate"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="195dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

• Mainactivity.java

public class MainActivity extends AppCompatActivity {


database db;
EditText ed1,ed2, ed3;
Button btnAdd, btnView,btnUpdate,btnDelete;

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

db = new database(this);
ed1=findViewById(R.id.editId);
ed2=findViewById(R.id.editName); ed3 =
findViewById(R.id.editAge); btnAdd =
findViewById(R.id.btnAdd);
btnUpdate = findViewById(R.id.btnupdate);
btnDelete = findViewById(R.id.btndelete);
btnView = findViewById(R.id.btnview);

btnAdd.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) {
String studentName = ed2.getText().toString();
int studentAge = Integer.parseInt(ed3.getText().toString());

if (db.insertStudent(studentName, studentAge)) {
showToast("Student added to the database.");
} else {
showToast("Error adding the student.");
}
}
});

btnDelete.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) {
String studentIdStr = ed1.getText().toString();

2|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY

if (studentIdStr.trim().isEmpty()) {
showToast("Please enter a valid student ID for deletion."); return;
}
int studentId = Integer.parseInt(studentIdStr);
boolean deleted = db.deleteStudent(studentId);

if (deleted) {
showToast("Student deleted from the database.");
} else {
showToast("Error deleting the student.");
}
}
});

btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String studentIdStr = ed1.getText().toString(); String
studentName = ed2.getText().toString(); String
studentAgeStr = ed3.getText().toString();

if (studentIdStr.trim().isEmpty() || studentName.trim().isEmpty() || studentAgeStr.trim().isEmpty()) {


showToast("Please enter valid student ID, name, and age.");
return;
}

int studentId = Integer.parseInt(studentIdStr);


int studentAge = Integer.parseInt(studentAgeStr);

boolean updated = db.updateStudent(studentId, studentName, studentAge); if (updated) {


showToast("Student information updated successfully.");
} else {
showToast("Error updating the student information.");
}
}
});

btnView.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) { Cursor
data = db.viewStudents();

if (data.getCount() == 0) {
showToast("No students in the database."); return;
}

StringBuilder students = new StringBuilder();


while (data.moveToNext()) {
students.append("ID: ").append(data.getInt(0)).append("\n");
students.append("Name: ").append(data.getString(1)).append("\n");
students.append("Age: ").append(data.getInt(2)).append("\n\n");
}
}
showToast(students.toString()
}
});
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();

3|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
}
}

• Datatabase.java

public class database extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "College.db";
private static final String TABLE_NAME = "Student"; private
static final String COL_ID = "ID";
private static final String COL_NAME = "NAME"; private
static final String COL_AGE = "AGE";
private static final int DB_VERSION = 1;

public database(Context context) {


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

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" + COL_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NAME +
" TEXT, " +
COL_AGE + " INTEGER)";
db.execSQL(createTable);
}

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

public boolean insertStudent(String name, int age) { SQLiteDatabase db


= this.getWritableDatabase(); ContentValues contentValues = new
ContentValues(); contentValues.put(COL_NAME, name);
contentValues.put(COL_AGE, age);
long result = db.insert(TABLE_NAME, null, contentValues); db.close();
return result != -1;
}

public boolean updateStudent(int studentId, String name, int age) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name); contentValues.put(COL_AGE,
age);
int rowsUpdated = db.update(TABLE_NAME, contentValues, COL_ID + " = ?", new String[]{String.valueOf(studentId)});
return rowsUpdated > 0;
}

public boolean deleteStudent(int studentId) { SQLiteDatabase db


= this.getWritableDatabase();
int rowsDeleted = db.delete(TABLE_NAME, COL_ID + " = ?", new String[]{String.valueOf(studentId)}); return
rowsDeleted > 0;
}

public Cursor viewStudents() {


SQLiteDatabase db = this.getReadableDatabase();

4|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
return db.query(TABLE_NAME, null, null, null, null, null, null);
}

• OUTPUT

2. Create an application that designs a layout having text boxes and button submit. The details of
doctor like doctor’s first name, last name, mobile number, address, city and specialization should
be entered by the user in the textboxes and on clicking on the button submit the data should be
saved into the database. Create another layout that contains a text box, a button search and a
text view that gives searching facility. User can search doctor’s full information by providing
doctor’s name in the text box. On clicking on button search the information of the doctor should
be displayed in text view. Doc_detail (doc_id, firstname, lastname, mob, add, city, specialization)

• activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"

5|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
android:text="Doctor Details"
android:textColor="@color/black"
android:textSize="25dp" android:textStyle="bold"
tools:ignore="MissingConstraints" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="@+id/edtFN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="first name" />
<EditText
android:id="@+id/edtLN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="last name" />

<EditText android:id="@+id/edtMoNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp" android:hint="mobile no" />
<EditText android:id="@+id/edtadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp" android:hint="address" />
<EditText android:id="@+id/edtcity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="city" />
<EditText android:id="@+id/edtSpecializ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp" android:hint="specialization"
/>

<Button android:id="@+id/btnp2submit"
android:layout_width="wrap_content"

6|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="submit" />

<Button android:id="@+id/btnp2search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="Search" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

• MainActivity.java

public class MainActivity extends AppCompatActivity { EditText


id,fName,lName,mNo,add,city,specialization; Button submit,search;
TextView tv;
DoctorDatabaseP2 doctorDatabaseP2;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

doctorDatabaseP2 = new DoctorDatabaseP2(this); fName =


(EditText) findViewById(R.id.edtFN); lName = (EditText)
findViewById(R.id.edtLN); mNo = (EditText)
findViewById(R.id.edtMoNo); add = (EditText)
findViewById(R.id.edtadd);
city = (EditText) findViewById(R.id.edtcity);
specialization = (EditText) findViewById(R.id.edtSpecializ); submit =
(Button) findViewById(R.id.btnp2submit); search = (Button)
findViewById(R.id.btnp2search);
submit.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
String firstName = fName.getText().toString(); String
lastName = lName.getText().toString(); String
mobileNo = mNo.getText().toString(); String address =
add.getText().toString(); String ct =
city.getText().toString();
String specializ = specialization.getText().toString(); try {
doctorDatabaseP2.insertData(firstName,lastName,mobileNo,address,ct,specializ);
Toast.makeText(MainActivity.this, "doctor added to the database.",
Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
Toast.makeText(MainActivity.this, "Error in adding doctor.", Toast.LENGTH_SHORT).show();
}
}
});

search.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View view) {
Intent intent = new Intent(MainActivity.this , searchp2.class); startActivity(intent);
}
});
}
}

7|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY

• activity_search_p2.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".searchp2">

<LinearLayout android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
<EditText android:id="@+id/edtsearch"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp" android:hint="Search doctor
name..." />
<Button android:id="@+id/btnp2search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="Search" />
</LinearLayout>
<TextView
android:id="@+id/tvsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" android:text="No more
search" android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/linearLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>

• searchp2.java

public class searchp2 extends AppCompatActivity { EditText

editTextSearch;
Button searchButton;
TextView textViewSearchResult; DoctorDatabaseP2
doctorDatabaseP2;
@SuppressLint("MissingInflatedId") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_p2);
doctorDatabaseP2 = new DoctorDatabaseP2(this);

editTextSearch = (EditText) findViewById(R.id.edtsearch); searchButton = (Button)


findViewById(R.id.btnp2search); textViewSearchResult = (TextView)
8|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
findViewById(R.id.tvsearch);
searchButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
String doctorName = editTextSearch.getText().toString(); Cursor cursor =
doctorDatabaseP2.searchDoctor(doctorName);
StringBuilder doctors = new StringBuilder(); while
(cursor.moveToNext()) {
@SuppressLint("Range") String firstName =
cursor.getString(cursor.getColumnIndex(DoctorDatabaseP2.COLUMN_FIRST_NAME));
@SuppressLint("Range") String lastName =
cursor.getString(cursor.getColumnIndex(DoctorDatabaseP2.COLUMN_LAST_NAME));
@SuppressLint("Range") String mobile =
cursor.getString(cursor.getColumnIndex(DoctorDatabaseP2.COLUMN_MOBILE));
@SuppressLint("Range") String address =
cursor.getString(cursor.getColumnIndex(DoctorDatabaseP2.COLUMN_ADDRESS));
@SuppressLint("Range") String city =
cursor.getString(cursor.getColumnIndex(DoctorDatabaseP2.COLUMN_CITY));
@SuppressLint("Range") String specialization =
cursor.getString(cursor.getColumnIndex(DoctorDatabaseP2.COLUMN_SPECIALIZATION));
String result = "Name: " + firstName + " " + lastName + "\n" + "Mobile: " + mobile
+ "\n" +
"Address: " + address + "\n" + "City:
" + city + "\n" +
"Specialization: " + specialization + "\n\n";
doctors.append(result);
}
textViewSearchResult.setText(doctors.toString());
}
});
}
}

• DoctorDatabaseP2.java

public class DoctorDatabaseP2 extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "DoctorInfo.db"; private static
final int DATABASE_VERSION = 1;
public static final String TABLE_DOCTOR = "Doc_detail"; public static
final String COLUMN_DOC_ID = "doc_id";
public static final String COLUMN_FIRST_NAME = "firstname"; public static
final String COLUMN_LAST_NAME = "lastname"; public static final String
COLUMN_MOBILE = "mob";
public static final String COLUMN_ADDRESS = "address"; public static
final String COLUMN_CITY = "city";
public static final String COLUMN_SPECIALIZATION = "specialization"; public
DoctorDatabaseP2(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); SQLiteDatabase db =
this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_DOCTOR + " (" + COLUMN_DOC_ID + " INTEGER
PRIMARY KEY AUTOINCREMENT, " + COLUMN_FIRST_NAME + " TEXT, " +
COLUMN_LAST_NAME + " TEXT, " +
COLUMN_MOBILE + " TEXT, " +
COLUMN_ADDRESS + " TEXT, " + COLUMN_CITY
+ " TEXT, " + COLUMN_SPECIALIZATION + "
9|Page
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
TEXT);";
db.execSQL(createTable);
}

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

public void insertData(String fName,String lName ,String moNo,String address,String city,String specialization ) {
SQLiteDatabase db = this.getWritableDatabase(); ContentValues
contentValues = new ContentValues();
contentValues.put(COLUMN_FIRST_NAME, fName);
contentValues.put( COLUMN_LAST_NAME,lName);
contentValues.put( COLUMN_MOBILE,moNo); contentValues.put(
COLUMN_ADDRESS,address); contentValues.put(
COLUMN_CITY,city);
contentValues.put( COLUMN_SPECIALIZATION,specialization);
db.insert(TABLE_DOCTOR, null, contentValues); db.close();
}
public Cursor searchDoctor(String name) { SQLiteDatabase db =
this.getReadableDatabase();
String sql = "SELECT * FROM " + TABLE_DOCTOR + " WHERE " + COLUMN_FIRST_NAME + " LIKE '%"
+ name + "%'" ;
return db.rawQuery(sql, null);
}
}

• OUTPUT

10 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
3. Create an application that designs a layout which provides field for enrollment number (text
box), student’s name (text box), course (radio buttons), semester (radio buttons)) and marks for
sub1 (text box), sub2 (text box), sub3 (text box) and a button name submit. When user clicks on
submit button data should be added to database.
• activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="10dp"
tools:context=".MainActivity">
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content" android:text="Student
registration" tools:ignore="MissingConstraints" />
<EditText android:id="@+id/enrollmentNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enrollment Number"

tools:ignore="MissingConstraints" />
<EditText android:id="@+id/studentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Student's Name"
/>

<RadioGroup
android:id="@+id/courseRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/studentName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.1">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Course"
tools:ignore="MissingConstraints" />
<RadioButton
android:id="@+id/bca"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true" android:text="BCA"/>

<RadioButton android:id="@+id/bscint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BSC(int)" />
11 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
<RadioButton android:id="@+id/bsccyber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BSC cyber" />
</RadioGroup>

<RadioGroup android:id="@+id/semesterRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/courseRadioGroup"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.1">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Student registration"
tools:ignore="MissingConstraints" />
<RadioButton android:id="@+id/semester1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="Semester 1" />
<RadioButton android:id="@+id/semester2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Semester 2" />

<RadioButton android:id="@+id/semester3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Semester 3" />
</RadioGroup>

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/semesterRadioGroup">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subject Marks" />
<EditText
android:id="@+id/sub1Marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="" android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/semesterRadioGroup" />

<EditText android:id="@+id/sub2Marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="" android:inputType="number"
app:layout_constraintTop_toBottomOf="@+id/sub1Marks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText android:id="@+id/sub3Marks"
android:layout_width="match_parent"
12 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
android:layout_height="wrap_content"
android:hint="" android:inputType="number"
app:layout_constraintTop_toBottomOf="@+id/sub2Marks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Submit Student"
app:layout_constraintTop_toBottomOf="@+id/sub3Marks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

<Button android:id="@+id/btnview"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="View"
app:layout_constraintTop_toBottomOf="@+id/submitBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

• MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText enrollmentNumber,studentName,sub1Marks,sub2Marks,sub3Marks; RadioGroup
courseRadioGroup,semesterRadioGroup; StudentDatabaseHelper db;
Button submitBtn,buttonview; @Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enrollmentNumber = findViewById(R.id.enrollmentNumber);
studentName = findViewById(R.id.studentName); sub1Marks =
findViewById(R.id.sub1Marks);
sub2Marks = findViewById(R.id.sub2Marks);
sub3Marks = findViewById(R.id.sub3Marks);
courseRadioGroup = findViewById(R.id.courseRadioGroup);
semesterRadioGroup = findViewById(R.id.semesterRadioGroup); db = new
StudentDatabaseHelper(this);
submitBtn = findViewById(R.id.submitBtn);

13 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
buttonview = findViewById(R.id.btnview);
submitBtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
String enrollment = enrollmentNumber.getText().toString(); String name =
studentName.getText().toString();
RadioButton selectedCourse = findViewById(courseRadioGroup.getCheckedRadioButtonId()); String course =
selectedCourse.getText().toString();
RadioButton selectedSemester = findViewById(semesterRadioGroup.getCheckedRadioButtonId());
String semester = selectedSemester.getText().toString(); int sub1 =
Integer.parseInt(sub1Marks.getText().toString()); int sub2 =
Integer.parseInt(sub2Marks.getText().toString()); int sub3 =
Integer.parseInt(sub3Marks.getText().toString());

SQLiteDatabase database = db.getWritableDatabase(); ContentValues values =


new ContentValues(); values.put(StudentDatabaseHelper.COL_ENROLLMENT,
enrollment); values.put(StudentDatabaseHelper.COL_NAME, name);
values.put(StudentDatabaseHelper.COL_COURSE, course);
values.put(StudentDatabaseHelper.COL_SEMESTER, semester);
values.put(StudentDatabaseHelper.COL_SUB1_MARKS, sub1);
values.put(StudentDatabaseHelper.COL_SUB2_MARKS, sub2);
values.put(StudentDatabaseHelper.COL_SUB3_MARKS, sub3);
long newRowId = database.insert(StudentDatabaseHelper.TABLE_NAME, null, values); if (newRowId != -
1) {
Toast.makeText(MainActivity.this, "Record added to the database.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Error adding record to the database.", Toast.LENGTH_SHORT).show();
}
}
});
buttonview.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, viewdata.class); startActivity(intent);
}
});
}
}

• activity_viewdata.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".viewdata">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
>

<TableLayout android:id="@+id/recordTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
14 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
<TableRow>
<TextView android:text="Enrollment"
android:gravity="center"
android:layout_weight="1"/>
<TextView android:text="Name"
android:gravity="center"
android:layout_weight="1"/>
<TextView android:text="Course"
android:gravity="center"
android:layout_weight="1"/>
<TextView android:text="Semester"
android:gravity="center"
android:layout_weight="1"/>
<TextView android:text="Total
Marks" android:gravity="center"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

• viewdata.java

public class viewdata extends AppCompatActivity {


TableLayout recordTable;
private StudentDatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewdata);
recordTable = findViewById(R.id.recordTable); db = new
StudentDatabaseHelper(this); displayRecords();
}

private void displayRecords() {


SQLiteDatabase database = db.getReadableDatabase(); String[]
projection = {
StudentDatabaseHelper.COL_ID,
StudentDatabaseHelper.COL_ENROLLMENT,
StudentDatabaseHelper.COL_NAME,
StudentDatabaseHelper.COL_COURSE,
StudentDatabaseHelper.COL_SEMESTER,
StudentDatabaseHelper.COL_SUB1_MARKS,
StudentDatabaseHelper.COL_SUB2_MARKS,
StudentDatabaseHelper.COL_SUB3_MARKS,
};

Cursor cursor = database.query( StudentDatabaseHelper.TABLE_NAME, projection,null, null,null, null, null


);
recordTable.removeAllViews(); if
(cursor != null) {
while (cursor.moveToNext()) { TableRow row =
new TableRow(this);

TextView enrollmentTextView = new TextView(this);


enrollmentTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(StudentDatabaseHelper. COL_ENROLLMENT)));
row.addView(enrollmentTextView);

15 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY

TextView nameTextView = new TextView(this);

nameTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(StudentDatabaseHelper.COL
_NAME)));
row.addView(nameTextView);

TextView courseTextView = new TextView(this);

courseTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(StudentDatabaseHelper.COL
_COURSE)));
row.addView(courseTextView);
TextView semesterTextView = new TextView(this);
semesterTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(StudentDatabaseHelper.C OL_SEMESTER)));
row.addView(semesterTextView);
int sub1Marks = cursor.getInt(cursor.getColumnIndexOrThrow(StudentDatabaseHelper.COL_SUB1_MARKS));
int sub2Marks = cursor.getInt(cursor.getColumnIndexOrThrow(StudentDatabaseHelper.COL_SUB2_MARKS));
int sub3Marks = cursor.getInt(cursor.getColumnIndexOrThrow(StudentDatabaseHelper.COL_SUB3_MARKS));

TextView totalMarksTextView = new TextView(this); totalMarksTextView.setText(String.valueOf(sub1Marks +


sub2Marks + sub3Marks)); row.addView(totalMarksTextView);
recordTable.addView(row);
}
cursor.close();
}
}
}

• StudentDatabaseHelper.java

public class StudentDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "StudentDatabase.db"; private static


final int DATABASE_VERSION = 2;
public static final String TABLE_NAME = "StudentRecords"; public static
final String COL_ID = "ID";
public static final String COL_ENROLLMENT = "Enrollment"; public static
final String COL_NAME = "Name";
public static final String COL_COURSE = "Course"; public static
final String COL_SEMESTER = "Semester";
public static final String COL_SUB1_MARKS = "Sub1Marks"; public static
final String COL_SUB2_MARKS = "Sub2Marks"; public static final String
COL_SUB3_MARKS = "Sub3Marks";
public StudentDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_ENROLLMENT + " TEXT, " +
COL_NAME + " TEXT, " + COL_COURSE + "
TEXT, " + COL_SEMESTER + " TEXT, " +
COL_SUB1_MARKS + " INTEGER, " +
COL_SUB2_MARKS + " INTEGER, " +
COL_SUB3_MARKS + " INTEGER)";
db.execSQL(sql);

16 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

• OUTPUT :

4. Create an application that designs a layout to store Result Details for BCA/BSC IT. The layout
must contain details like name (textbox), Sem 1 Marks(spinner), Sem 2 Marks(spinner), Sem 3
Marks(spinner) , Sem 4 Marks(spinner), Sem 5 Marks(spinner) , Sem 6 Marks(spinner) and a
Button named Submit.
Note: Marks must be from (AA, AB, BB, BC, CC and FF) When the submit button is selected all the details should be
saved in the database.
Also provide a layout to perform the following operations:
a. Update the Marks/grade.
b. Search the Students on basis of his marks / grade.

• Activity_main.Xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">

17 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<Spinner android:id="@+id/spinnerSem1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/marks_array" />

<EditText android:id="@+id/etNewMarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Marks" />
<Spinner android:id="@+id/spinnerSearchMarks"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

<Button android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update Marks" />
<Button android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Students" />

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

<EditText android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Your Id"
android:inputType="text" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

• MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
18 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter; import
android.widget.Button; import
android.widget.EditText; import
android.widget.Spinner; import
android.widget.Toast;

public class MainActivity extends AppCompatActivity { EditText

eName, eNewMarks;
Spinner spinnersem1, spinnerSearchMarks;
Button btnSubmit, btnUpdate, btnSearch, btnView;
ArrayAdapter<String> marksAdapter; DatabaseHelperquesion4 db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

eName = findViewById(R.id.etName); spinnersem1 =


findViewById(R.id.spinnerSem1); btnSubmit =
findViewById(R.id.btnSubmit); eNewMarks =
findViewById(R.id.etNewMarks); btnUpdate =
findViewById(R.id.btnUpdate);
spinnerSearchMarks = findViewById(R.id.spinnerSearchMarks); btnSearch =
findViewById(R.id.btnSearch);
btnView = findViewById(R.id.btnView); db = new
DatabaseHelperquesion4(this);
marksAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.marks_array));
marksAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSearchMarks.setAdapter(marksAdapter);

btnSubmit.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) {
saveResultDetails();
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
updateMarks(eName.getText().toString(), eNewMarks.getText().toString());
}
});

btnSearch.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) { searchStudentsByMarks(spinnerSearchMarks.getSelectedItem().toString());
}
});
btnView.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { viewAllStudents();
}
});
}
private void saveResultDetails() {
String name = eName.getText().toString();
String sem1Marks = spinnersem1.getSelectedItem().toString();
SQLiteDatabase database = db.getWritableDatabase(); ContentValues
values = new ContentValues(); values.put("name", name);

19 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
values.put("sem1_marks", sem1Marks);
long newRowId = database.insert("result_details", null, values); if
(newRowId != -1) {
showToast("Data inserted successfully.");
} else {
showToast("Error occurred while inserting data.");
}
}
private void updateMarks(String studentName, String newMarks) { SQLiteDatabase
database = db.getWritableDatabase(); ContentValues values = new
ContentValues(); values.put("sem1_marks", newMarks);
String selection = "name=?";
String[] selectionArgs = {studentName};

int rowsUpdated = database.update("result_details", values, selection, selectionArgs); if (rowsUpdated >

0) {

showToast("Marks updated successfully.");


} else {
showToast("Error occurred while updating marks.");
}
}
private void searchStudentsByMarks(String marks) {
SQLiteDatabase database = db.getReadableDatabase(); String[]
projection = {
"name", "sem1_marks"
};
String selection = "sem1_marks=?"; String[]
selectionArgs = {marks};

Cursor cursor = database.query("result_details", projection, selection, selectionArgs, null, null, null);


StringBuilder studentsList = new StringBuilder();
if (cursor.moveToFirst()) {
int nameColumnIndex = cursor.getColumnIndex("name");
int sem1MarksColumnIndex = cursor.getColumnIndex("sem1_marks");
do {
String studentName = cursor.getString(nameColumnIndex); String
sem1Marks = cursor.getString(sem1MarksColumnIndex);

studentsList.append("Name: ").append(studentName).append(", Sem1 Marks:


").append(sem1Marks).append("\n");
} while (cursor.moveToNext());
} else {
studentsList.append("No students found.");
}
showToast(studentsList.toString());
}

private void viewAllStudents() { Cursor cursor


= db.viewStudents();
StringBuilder studentsList = new StringBuilder();
if (cursor.moveToFirst()) {
int idColumnIndex = cursor.getColumnIndex("id");
int nameColumnIndex = cursor.getColumnIndex("name");
int sem1MarksColumnIndex = cursor.getColumnIndex("sem1_marks");
do {
if (idColumnIndex != -1) {

20 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
int id = cursor.getInt(idColumnIndex); studentsList.append("ID:
").append(id).append(", ");
}
if (nameColumnIndex != -1) {
String name = cursor.getString(nameColumnIndex); studentsList.append("Name:
").append(name).append(", ");
}
if (sem1MarksColumnIndex != -1) {
String sem1Marks = cursor.getString(sem1MarksColumnIndex);
studentsList.append("Sem1 Marks: ").append(sem1Marks);
}
studentsList.append("\n");
} while (cursor.moveToNext());
} else {
studentsList.append("No students found.");
}
showToast(studentsList.toString());
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

• Array.xml

<?xml version="1.0" encoding="utf-8"?>


<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string-array name="marks_array">
<item>AA</item>
<item>AB</item>
<item>BB</item>
<item>BC</item>
<item>CC</item>
<item>FF</item>
</string-array>
</resources>

• DatabaseHelperquesion4.xml

public class DatabaseHelperquesion4 extends SQLiteOpenHelper { private static


final String DATABASE_NAME = "ResultDetails.db"; private static final String
TABLE_NAME = "result_details"; private static final int DATABASE_VERSION
= 1;
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_SEM1_MARKS = "sem1_marks";
public DatabaseHelperquesion4(@Nullable Context context) { super(context,
DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE
TABLE " + TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NAME
+ " TEXT, " +
COL_SEM1_MARKS + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
21 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public Cursor viewStudents() {


SQLiteDatabase db = this.getReadableDatabase();
return db.query(TABLE_NAME, null, null, null, null, null, null);
}
}

• OUTPUT

5. Create an application that designs a layout to store Student Details. The layout should contain
information like: Roll number (textbox), Name (textbox), Birth date (Date Picker), Marks1
(textbox), Marks2 (textbox), Marks3 (textbox) and button submit. When button submit is clicked
than all the details must be saved in the database. Also percentage and total should be
calculated automatically and saved into the database.
Also design a layout to perform following operations:
a) Update Any student’s marks
b) Search the student details by roll no
c) Delete any student by roll no

• Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">

22 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/edRollNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:hint="Roll no"/>

<EditText
android:id="@+id/edSName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp" android:hint="Student
name" />
<TextView android:id="@+id/edTest"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp" android:hint="Select Date"
/>
<EditText android:id="@+id/edMarks1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp" android:hint="Marks 1"/>
<EditText android:id="@+id/edMarks2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp" android:hint="Marks 2" />
<EditText android:id="@+id/edMarks3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp" android:hint="Marks 3" />
<Button android:id="@+id/btnP5submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="Submit"/>
<Button android:id="@+id/searchP5t"

23 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="Search"/>
<Button android:id="@+id/updateP5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="update"/>
<Button android:id="@+id/deleteP5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#008080" android:text="delete"/>

</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

• MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity; import

android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle; import
android.view.View; import
android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText; import
android.widget.TextView; import
android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity { EditText


rollno,stdname,mrk1,mrk2,mrk3;
TextView dataText;
Button submit,search,updated,deleted; TextView
tvs;
DatabaseHelpers dbhelper;
@SuppressLint("MissingInflatedId") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dbhelper=new DatabaseHelpers(this);
rollno=findViewById(R.id.edRollNo);
stdname=findViewById(R.id.edSName);
dataText=findViewById(R.id.edTest);
mrk1=findViewById(R.id.edMarks1);
mrk2=findViewById(R.id.edMarks2);
mrk3=findViewById(R.id.edMarks3);
submit=findViewById(R.id.btnP5submit);

24 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
search=findViewById(R.id.searchP5t);
updated=findViewById(R.id.updateP5);
deleted=findViewById(R.id.deleteP5);

final Calendar calendar = Calendar.getInstance(); final int year


= calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);

dataText.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View view) {
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
{
i1 = i1+1;
String date= day + "/" + month + "/" + year;

dataText.setText(date);
}
},year,month,day);
dialog.show();
}
});
String dates = day + "/" + month + "/" + year;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Rollno = rollno.getText().toString();
String studentname = stdname.getText().toString(); String m1 =
mrk1.getText().toString();
String m2 = mrk2.getText().toString(); String m3
= mrk3.getText().toString();
int total = Integer.parseInt(m1) + Integer.parseInt(m2) + Integer.parseInt(m3); double per =
total/3;

try {
dbhelper.insertData(Rollno,studentname,dates,m1,m2,m3,total,per);
Toast.makeText(MainActivity.this,"Student Result added to the database.",Toast.LENGTH_SHORT).show();
}catch (Exception exception)
{
Toast.makeText(MainActivity.this,"Error in adding Student Result.",Toast.LENGTH_SHORT).show();
}
}
});
search.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, searchP5.class); startActivity(intent);
}
});

updated.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View view) {
String studentId = rollno.getText().toString(); String
marks1 = mrk1.getText().toString(); String marks2 =
mrk2.getText().toString(); String marks3 =
mrk3.getText().toString();
25 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
if (studentId.trim().isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter a valid student ID for updating.", Toast.LENGTH_SHORT).show();
return;
} else { try
{
dbhelper.updateStudent(studentId, marks1,marks2,marks3); Toast.makeText(MainActivity.this, "Student
information updated successfully.",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Error updating the student information.", Toast.LENGTH_SHORT).show();
}
}
}
});

updated.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View view) {
String studentId = rollno.getText().toString(); String
marks1 = mrk1.getText().toString(); String marks2 =
mrk2.getText().toString(); String marks3 =
mrk3.getText().toString();

if (studentId.trim().isEmpty()){
Toast.makeText(MainActivity.this,"Please enter a valid student ID for updating.",Toast.LENGTH_SHORT).show();
return;
}else {
try {
dbhelper.updateStudent(studentId,marks1,marks2,marks3); Toast.makeText(MainActivity.this, "Student
information updated successfully.",
Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(MainActivity.this, "Error updating the student information.", Toast.LENGTH_SHORT).show();
}
}
}
});

deleted.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View view) {
String studentId = rollno.getText().toString(); if
(studentId.trim().isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter a valid student ID for deletion.", Toast.LENGTH_SHORT).show();
return;
}
try {

dbhelper.deleteStudent(studentId);
Toast.makeText(MainActivity.this, "Student deleted from the database.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Error deleting the student.", Toast.LENGTH_SHORT).show();
}

}
});

26 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
}
}
• activity_search_p5.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".searchP5">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp">

<EditText android:id="@+id/edtSearchP5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="search Roll Number ..."
/>

<Button android:id="@+id/btnP5Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"

android:backgroundTint="#008080" android:text="Search" />

</LinearLayout>

<TextView android:id="@+id/tvSearchP5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:text="no more search"
android:textColor="@color/black"
android:textSize="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

• searchP5.java

public class searchP5 extends AppCompatActivity { EditText


editTextSearch;
Button searchButton;
TextView textViewSearchResult; DatabaseHelpers

27 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
dbHelper;

@SuppressLint("MissingInflatedId") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_p5);

dbHelper = new DatabaseHelpers(this);


editTextSearch = (EditText) findViewById(R.id.edtSearchP5); searchButton =
(Button) findViewById(R.id.btnP5Search); textViewSearchResult = (TextView)
findViewById(R.id.tvSearchP5); searchButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editTextSearch.getText().toString(); Cursor cursor =
dbHelper.searchStudentResult(name);

StringBuilder students = new StringBuilder(); if


(cursor.moveToFirst()) {
do {

@SuppressLint("Range") String roll_no = cursor.getString(cursor.getColumnIndex(ROLL_NO));


@SuppressLint("Range") String stdName = cursor.getString(cursor.getColumnIndex(STUDENT_NAME));
@SuppressLint("Range") String BDAY = cursor.getString(cursor.getColumnIndex(BIRTHDAY_DATE));
@SuppressLint("Range") int mrks1 = cursor.getInt(cursor.getColumnIndex(MARKS1));
@SuppressLint("Range") int mrks2 = cursor.getInt(cursor.getColumnIndex(MARKS2));
@SuppressLint("Range") int mrks3 = cursor.getInt(cursor.getColumnIndex(MARKS3));
@SuppressLint("Range") int totals = cursor.getInt(cursor.getColumnIndex(TOTAL)); @SuppressLint("Range")
int pers = cursor.getInt(cursor.getColumnIndex(PERCENTAGE));

// You can format the result as needed


String result = "Enrollment_No : " + roll_no + "\n" + "StdName
: " + stdName + "\n" + "Birthday_date : " + BDAY + "\n" +
"Marks1 : " + mrks1 + "\n" + "Marks2 :
" + mrks2 + "\n" + "Marks3 : " + mrks3
+ "\n" + "Total : " + totals + "\n" +
"Percentage : " + pers +"\n\n" ;
textViewSearchResult.setText(result);

} while (cursor.moveToNext());
}
}
});

}
}

• DatabaseHelpers.java

public class DatabaseHelpers extends SQLiteOpenHelper { private static


final String DATABASE_NAME = "Result_db"; private static final int
DATABASE_VERSION = 1;

public static final String Student_details = "student_result"; public static


final String Student_id = "student_id";
public static final String ROLL_NO = "roll_no";

28 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
public static final String STUDENT_NAME = "student_name";
public static final String BIRTHDAY_DATE = "birthday_date"; public static
final String MARKS1 = "marks1";
public static final String MARKS2 = "marks2"; public
static final String MARKS3 = "marks3"; public static
final String TOTAL = "total";
public static final String PERCENTAGE = "percentage";

public DatabaseHelpers(Context context) {


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

@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "CREATE TABLE IF NOT EXISTS " + Student_details + " (" + Student_id + " INTEGER PRIMARY
KEY AUTOINCREMENT, " + ROLL_NO + " TEXT, " + STUDENT_NAME + " TEXT , " + BIRTHDAY_DATE + " TEXT , " + MARKS1 + "
TEXT , " + MARKS2 + " TEXT , " + MARKS3 + " TEXT , " + TOTAL + " INTEGER , " + PERCENTAGE + " DOUBLE);";
db.execSQL(createTableQuery);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

public void insertData(String roll_no, String std_name, String BDay, String mrk1, String mrk2, String mrk3, int total, double
per) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();


contentValues.put(ROLL_NO, roll_no);
contentValues.put(STUDENT_NAME, std_name);
contentValues.put(BIRTHDAY_DATE, BDay);
contentValues.put(MARKS1, mrk1); contentValues.put(MARKS2,
mrk2); contentValues.put(MARKS3, mrk3);
contentValues.put(TOTAL, total); contentValues.put(PERCENTAGE,
per); db.insert(Student_details, null, contentValues); db.close();
}
public Cursor searchStudentResult(String roll_no) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + Student_details + " WHERE " + ROLL_NO + " LIKE '%" + roll_no + "%'";

return db.rawQuery(sql, null);


}

public void updateStudent(String rollNo, String mrk1, String mrk2, String mrk3) { SQLiteDatabase db
= this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MARKS1, mrk1); contentValues.put(MARKS2,
mrk2); contentValues.put(MARKS3, mrk3);
db.update(Student_details, contentValues, ROLL_NO + "=?", new String[]{String.valueOf(rollNo)}); db.close();
}
public void deleteStudent(String name) { SQLiteDatabase db =
this.getWritableDatabase();
db.delete(Student_details, ROLL_NO + "=?", new String[]{String.valueOf(name)});

29 | P a g e
Class: TYBCA SEM-5 Subject: MOBILE APPLICATION DEVELOPMENT
Author: SYSTEM Enrollment No: 210202010_ _ _ _
Date: DD-MM-YYYY
}

• OUTPUT

30 | P a g e

You might also like