Sunday, November 2, 2025

MCA 1 Android Using Kotlin Unit 5 : Layouts and Database Connectivity Using SQLite

Prepared By : Prof. Uday Shah (HOD-IT)

Unit 5 : Layouts and Database Connectivity Using SQLite 

1. Linear Layout 1

2. Frame Layout 1

3. Relative Layout 2

4. Constraint Layout 2

5. Managing Data Using SQLite. 3

6. Insert Operation in SQLite. 3

7. Update Operation in SQLite. 4

8. Delete Operation in SQLite. 4

9. Select Operation in SQLite. 5

10. Cursor Usage. 5

11. ContentValues Usage. 6

12. Creating Database Helper Class. 6

13. Data Storage APIs. 7

 

 1. Linear Layout

  • LinearLayout arranges child views in a single direction: either vertical or horizontal.
  • Direction is set using android:orientation="vertical" or "horizontal".
  • Commonly used for form designs, login pages, column/row alignment.
  • Views are placed one after another, like a stack.
  • Supports weight (layout_weight) for proportional distribution of space.
  • Example XML:
  • <LinearLayout android:orientation="vertical"></LinearLayout>
  • Simple to understand and easy to design.
  • Uses more nesting for complex designs, which can reduce performance.
  • Supports gravity and layout_gravity for alignment.
  • Best for small layouts with few elements.
  • Works well with ScrollView for long forms.
  • Offers margin/padding flexibility.
  • Lightweight and efficient for simple UI screens.

 2. Frame Layout

  • FrameLayout is designed to stack multiple views on top of each other.
  • The first child is at the bottom, and the last child appears on top.
  • Mostly used for single child screens like splash screen or fragment container.
  • Commonly used for Fragment transactions as a container.
  • XML example:
  • <FrameLayout android:id="@+id/container"></FrameLayout>
  • Allows overlapping of views, useful for badges, overlays, and watermarks.
  • Simple and lightweight compared to other layout types.
  • Does not support complex positioning by default.
  • Very useful in custom UI designs.
  • Supports foreground drawable for overlay effects.
  • Often used inside ConstraintLayout or RelativeLayout as a placeholder.
  • Helps create image overlays or layered UI designs.

 3. Relative Layout

  • RelativeLayout positions elements relative to each other or parent.
  • Allows alignment like below, above, toRightOf, toLeftOf, etc.
  • Makes UI flexible and reduces nesting compared to LinearLayout.
  • XML example:
  • <RelativeLayout></RelativeLayout>
  • Supports parent-relative positioning: alignParentTop, alignParentLeft, etc.
  • Good for medium-level complex designs where element relationships matter.
  • Harder to maintain when layout becomes large.
  • Widely used before ConstraintLayout became modern standard.
  • Supports center alignment easily.
  • Helps reduce layout depth compared to nested LinearLayouts.
  • Works with margins, padding, and wrap/match constraints.
  • Supports floating elements like images over text.

 4. Constraint Layout

  • ConstraintLayout is the most powerful and modern layout for Android.
  • Allows positioning views using anchor points called constraints.
  • Greatly reduces nesting → improves performance.
  • Can create complex UI designs with a flat view hierarchy.
  • Supports guidelines, barriers, biasing, chains, and ratio-based resizing.
  • XML example:
  • <ConstraintLayout></ConstraintLayout>
  • Works with Android Studio’s Layout Editor visually.
  • Best layout for modern app designs.
  • Supports responsive UI across screen sizes.
  • Chains help align views horizontally/vertically.
  • Bias controls placement along constraints.
  • Recommended for all professional Android apps.
  • Easy to manage with blueprint UI editor.

 5. Managing Data Using SQLite

  • SQLite is a lightweight, embedded database in Android.
  • Used for storing structured data in table format.
  • Data remains saved even after app restarts.
  • Supports SQL commands: CREATE, INSERT, UPDATE, DELETE, SELECT.
  • Fast, reliable, and does not require a server.
  • Used for offline apps like notes, expenses, contacts.
  • Accessed through SQLiteDatabase class in Android.
  • Stores data in .db file inside internal storage.
  • Can manage multiple tables and relationships.
  • Works with Cursor to read results.
  • Requires a helper class (SQLiteOpenHelper) for database management.
  • More secure than SharedPreferences for storing multiple entries.
  • Supports transactions for better performance.

 6. Insert Operation in SQLite

  • Insert operation is used to add new data to a table.
  • Implemented using SQL INSERT query or using ContentValues.
  • Example using ContentValues:
  • val cv = ContentValues()
  • cv.put("name", "Uday")
  • db.insert("student", null, cv)
  • Requires writable database using db = writableDatabase.
  • Helps store user data like form entries.
  • Can insert multiple rows using loops or bulk insert.
  • Auto-increment columns work automatically.
  • Returns row ID of newly inserted row.
  • Can validate data before inserting.
  • Insert errors can be handled using try-catch.
  • Used in registration forms, saving notes, adding records.

 7. Update Operation in SQLite

  • Update modifies existing data in a table.
  • Requires identifying which record to update using WHERE clause.
  • Example:
  • val cv = ContentValues()
  • cv.put("name", "Updated Name")
  • db.update("student", cv, "id = ?", arrayOf("1"))
  • Only selected rows change; others remain unchanged.
  • Used when editing profile, updating record values.
  • Returns number of rows affected.
  • Must check if record exists before updating.
  • Requires writable database.
  • Should avoid updating primary keys.
  • Useful in CRUD applications.

 8. Delete Operation in SQLite

  • Delete removes specific rows from a table.
  • Uses SQL DELETE command or database delete method.
  • Example:
  • db.delete("student", "id = ?", arrayOf("1"))
  • Must use WHERE clause; else entire table will delete.
  • Returns number of rows deleted.
  • Useful for remove, clean-up, and record management.
  • Can delete based on conditions (e.g., delete expired records).
  • Must handle foreign key constraints if exists.
  • Requires writable database.

 9. Select Operation in SQLite

  • Select retrieves data from the database.
  • Uses SQL SELECT query.
  • Example:
  • db.rawQuery("SELECT * FROM student", null)
  • Returns result in Cursor format.
  • Used to display data in ListView, RecyclerView.
  • Can filter data using WHERE clause.
  • Supports sorting using ORDER BY.
  • Cursor must be closed after use.
  • Readable database is used for select queries.
  • Can fetch specific fields or all fields.

 10. Cursor Usage

  • Cursor is the result-set returned from SELECT query.
  • Acts like a pointer to rows in the table.
  • Moves row-by-row using moveToNext().
  • Example:
  • val c = db.rawQuery("SELECT * FROM student", null)
  • while (c.moveToNext()) { }
  • Can get data using getString(), getInt(), etc.
  • Must close cursor after use: c.close().
  • Helps read each column of each row.
  • Used for displaying data in list or adapter.
  • Provides row count using getCount().
  • Essential for retrieving database records.

 11. ContentValues Usage

  • ContentValues stores data as key–value pairs.
  • Used for INSERT and UPDATE operations.
  • Example:
  • val cv = ContentValues()
  • cv.put("name", "Raj")
  • Ensures safe data handling without writing raw SQL.
  • Prevents SQL injection attacks.
  • Works with INT, TEXT, FLOAT, LONG, DOUBLE values.
  • Must match column names accurately.
  • Makes CRUD operations easier.
  • Used widely in Android SQLite programming.
  • Increases readability and cleaner code.

 12. Creating Database Helper Class

  • Database Helper class extends SQLiteOpenHelper.
  • Manages database creation and version upgrades.
  • Must override: onCreate() and onUpgrade().
  • Example:
  • class DBHelper(context: Context): SQLiteOpenHelper(context, "mydb", null, 1)
  • onCreate() executes CREATE TABLE query.
  • Provides readable and writable database objects.
  • Handles version update using onUpgrade().
  • Makes database code reusable.
  • Required for every SQLite-based app.
  • Helps organize database operations.

 13. Data Storage APIs

  • Android provides various APIs for storing data.
  • Includes SharedPreferences, SQLite, Room Database, Files, and Cloud Storage.
  • SharedPreferences is for small key–value data.
  • SQLite is for structured relational data.
  • Room is modern ORM on top of SQLite.
  • Internal Storage stores private files.
  • External Storage stores public files.
  • Data can also be stored in JSON, XML files.
  • Helps choose correct storage based on app need.
  • Ensures long-term data persistence.
  • Provides secure and reliable data management.

 

 :: Best of Luck ::