Unit 1: Android app development practice
Software Lab-X (Mobile Application Development) notes · PTU syllabus (BSIT604)
On this page
- Unit summary
- Installing the SDK and IDE
- Creating an Android emulator
- Supporting multiple screen sizes
- Alert dialogs and custom dialogs
- String arrays and custom lists
- Spinner and ListView
- SQLiteOpenHelper and the SQL API
- SQLiteDatabase and Cursor with a ListView
- Reading and updating contacts and bookmarks
- CRUD app with GridView and ListView
- Practical list
- Key terms
- Quick revision
- Important questions
Unit summary
This lab builds Android apps step by step: installing the SDK and creating an emulator, supporting multiple screen sizes, alert and custom dialogs, string arrays and custom lists, SQLiteOpenHelper and the SQL API, spinners and list views, cursors, reading contacts, and finally a complete CRUD app displaying records in a GridView and ListView.
After this unit you can
- Set up the Android SDK, IDE and emulator
- Build layouts that adapt to screen sizes and show dialogs and lists
- Store and query data with SQLite
- Build a CRUD app and read contacts through a content provider
PTU syllabus topics
- Installing the SDK
- creating an Android emulator
- installing Eclipse and Android Development Tools
- supporting multiple screen sizes
- alert/custom dialogs
- string arrays and custom lists
- SQLiteOpenHelper
- SQL API
- spinner and list view
- SQLiteDatabase and cursor
- reading/updating contacts and bookmarks
- building a CRUD app with GridView and ListView display
- 1
Create the project
Android Studio
- 2
Design layouts
XML: ListView or GridView
- 3
Create the SQLite helper
- 4
Insert, read, update, delete
- 5
Show data in a list
- 6
Test on the emulator
Topic 1
Installing the SDK and IDE
- The syllabus names Eclipse with the Android Development Tools (ADT) plug-in; ADT was discontinued in 2015 and Android Studio (which bundles the SDK, emulator and Gradle build system) is now the official IDE.
- 1Download Android Studio from developer.android.com
- 2Run the setup wizard — it installs the Android SDK, platform tools and an emulator image
- 3Open SDK Manager to add platform versions and build tools
- 4Create a new project — Empty Views Activity, language Java, minimum SDK (e.g., API 24)
- 5Wait for Gradle sync, then run
Topic 2
Creating an Android emulator
- 1Tools → Device Manager → Create device
- 2Choose hardware profile (e.g., Pixel phone or a tablet)
- 3Choose a system image (API level, x86_64 with Google APIs)
- 4Name the AVD and set orientation, RAM
- 5Start the emulator and run the app (Shift + F10)
- Physical device: enable Developer options and USB debugging, connect by USB or Wi-Fi pairing; the device appears in the run target list.
Topic 3
Supporting multiple screen sizes
xml<!-- res/layout/activity_main.xml: ConstraintLayout adapts to width -->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:id="@+id/btnStart" android:text="@string/start"
android:layout_width="0dp" android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" android:layout_margin="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>- Create res/layout-land and res/layout-sw600dp versions of the same file for landscape and tablets; Android picks the best match. Test on phone, tablet and rotated emulators.
Topic 4
Alert dialogs and custom dialogs
java// Alert dialog with a single-choice list
String[] courses = getResources().getStringArray(R.array.courses);
new AlertDialog.Builder(this)
.setTitle("Choose course")
.setSingleChoiceItems(courses, -1, (d, which) -> chosen = courses[which])
.setPositiveButton("OK", (d, w) -> Toast.makeText(this, chosen, Toast.LENGTH_SHORT).show())
.show();
// Custom dialog with its own layout
Dialog dlg = new Dialog(this);
dlg.setContentView(R.layout.dialog_login);
EditText user = dlg.findViewById(R.id.etUser);
dlg.findViewById(R.id.btnOk).setOnClickListener(v -> { /* validate */ dlg.dismiss(); });
dlg.show();Topic 5
String arrays and custom lists
xml<!-- res/values/arrays.xml -->
<resources>
<string-array name="courses">
<item>BCA</item><item>B.Sc IT</item><item>BBA</item><item>MBA</item>
</string-array>
</resources>java// Custom list row: image + title + subtitle
public class CourseAdapter extends ArrayAdapter<Course> {
public CourseAdapter(Context c, List<Course> list) { super(c, 0, list); }
@Override public View getView(int pos, View row, ViewGroup parent) {
if (row == null) row = LayoutInflater.from(getContext()).inflate(R.layout.row_course, parent, false);
Course c = getItem(pos);
((TextView) row.findViewById(R.id.tvTitle)).setText(c.name);
((TextView) row.findViewById(R.id.tvSeats)).setText(c.seats + " seats");
((ImageView) row.findViewById(R.id.ivIcon)).setImageResource(c.icon);
return row;
}
}Topic 6
Spinner and ListView
javaSpinner sp = findViewById(R.id.spCourse);
ArrayAdapter<CharSequence> ad = ArrayAdapter.createFromResource(this, R.array.courses,
android.R.layout.simple_spinner_item);
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(ad);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> p, View v, int pos, long id) { selected = p.getItemAtPosition(pos).toString(); }
public void onNothingSelected(AdapterView<?> p) { }
});Topic 7
SQLiteOpenHelper and the SQL API
javapublic class StudentDB extends SQLiteOpenHelper {
public StudentDB(Context c) { super(c, "students.db", null, 1); }
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE student(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, course TEXT, marks INTEGER)");
}
public void onUpgrade(SQLiteDatabase db, int o, int n) { db.execSQL("DROP TABLE IF EXISTS student"); onCreate(db); }
public long add(String name, String course, int marks) {
ContentValues cv = new ContentValues();
cv.put("name", name); cv.put("course", course); cv.put("marks", marks);
return getWritableDatabase().insert("student", null, cv);
}
public int update(long id, int marks) {
ContentValues cv = new ContentValues(); cv.put("marks", marks);
return getWritableDatabase().update("student", cv, "_id=?", new String[]{String.valueOf(id)});
}
public int delete(long id) {
return getWritableDatabase().delete("student", "_id=?", new String[]{String.valueOf(id)});
}
public Cursor all() {
return getReadableDatabase().rawQuery("SELECT * FROM student ORDER BY name", null);
}
}Topic 8
SQLiteDatabase and Cursor with a ListView
javaCursor c = db.all();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c,
new String[]{"name", "course"}, new int[]{android.R.id.text1, android.R.id.text2}, 0);
ListView lv = findViewById(R.id.lvStudents);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener((p, v, pos, id) -> { // id is the _id column
db.delete(id);
adapter.changeCursor(db.all()); // refresh
return true;
});- A cursor used with SimpleCursorAdapter must have an _id column.
Topic 9
Reading and updating contacts and bookmarks
java// Manifest: <uses-permission android:name="android.permission.READ_CONTACTS"/>
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
return;
}
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
List<String> list = new ArrayList<>();
while (c.moveToNext()) {
list.add(c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) + " " +
c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
c.close();- Updating contacts needs WRITE_CONTACTS and ContentProviderOperation batches. Browser bookmarks: the old public Browser bookmarks provider was removed in Android 6.0, so modern labs practise the same provider pattern with contacts, the call log or MediaStore instead.
Topic 10
CRUD app with GridView and ListView
- 1
Layout
EditTexts for name, course (Spinner) and marks; Add, Update, Delete, View buttons
- 2
Database
StudentDB helper with add, update, delete, all
- 3
Validation
Show a Toast or error for empty or invalid input
- 4
Display in ListView
SimpleCursorAdapter or custom adapter
- 5
Display in GridView
Course tiles with student counts (numColumns 2 or auto_fit)
- 6
Confirm deletes
AlertDialog before deleting
xml<GridView android:id="@+id/gvCourses"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:numColumns="auto_fit" android:columnWidth="140dp"
android:verticalSpacing="8dp" android:horizontalSpacing="8dp"/>Topic 11
Practical list
- Install the SDK and IDE; create emulators for a phone and a tablet.
- Hello app; activity life-cycle logging with Logcat.
- Layout supporting portrait, landscape and tablet sizes.
- Alert dialog, single and multiple choice dialogs, date picker, custom login dialog.
- String array in a Spinner and ListView; custom list with images.
- SQLite database with SQLiteOpenHelper: insert, update, delete, view.
- Display records with SQLiteDatabase and Cursor in a ListView.
- Read contacts with run-time permission.
- CRUD app with GridView and ListView display.
Key terms
- AVD
- Android Virtual Device used by the emulator
- Android Studio
- Official IDE for Android development
- String array resource
- List of strings defined in XML
- SimpleCursorAdapter
- Adapter binding cursor columns to list views
- Run-time permission
- Dangerous permission requested while the app runs
Quick revision
- Android Studio replaces Eclipse ADT; SDK Manager; AVD.
- ConstraintLayout; layout-land and layout-sw600dp.
- AlertDialog and custom Dialog.
- string-array, ArrayAdapter, custom adapter, Spinner, ListView, GridView.
- SQLiteOpenHelper CRUD; Cursor with _id; contacts provider and permissions.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What is an AVD?
- Q2.How do you supply a separate layout for tablets?
- Q3.How is a string array defined in XML?
- Q4.Why must a cursor have an _id column for SimpleCursorAdapter?
- Q5.Which permission is needed to read contacts?
- Q6.Distinguish ListView and GridView.
Long-answer questions
- Q1.Explain the steps to set up Android Studio and create an emulator.
- Q2.Write an app showing alert and custom dialogs.
- Q3.Write an app to insert, update, delete and display records using SQLite.
- Q4.Write an app to read contacts and display them in a ListView.
Stuck on this unit?
Message SBS on WhatsApp for help with Software Lab-X (Mobile Application Development), or to ask about studying B.Sc IT at Synetic.
