Unit 2: UI widgets and data storage
Mobile Application Development notes · PTU syllabus (BSIT601)
On this page
Unit summary
Users interact through widgets, and apps must remember data. This unit covers text and button controls, toggle buttons, images, notifications and toasts, menus and dialogs, custom lists, Google Maps and GPS location, shared preferences, file access, SQLite, and view and drawable animation.
After this unit you can
- Build user interfaces with common widgets
- Show toasts, notifications, menus and dialogs
- Create custom lists and use maps and location
- Store data in preferences, files and SQLite and add animations
PTU syllabus topics
- Text and button controls
- toggle buttons
- images
- notifications and toast
- menus and dialogs
- custom lists
- Google Maps and GPS location
- shared preferences
- file access
- SQLite database
- view and drawable animation
Shared preferences
Small key-value settings
Internal storage
Private files
External storage
Shared files, media
SQLite
Structured local database
Network
Web services, cloud
Topic 1
Text and button controls
xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="16dp"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:text="Student name" android:textSize="18sp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<EditText android:id="@+id/etName" android:hint="Enter name" android:inputType="textPersonName"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btnGreet" android:text="Greet"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>javaEditText et = findViewById(R.id.etName);
Button b = findViewById(R.id.btnGreet);
b.setOnClickListener(v -> Toast.makeText(this, "Hello " + et.getText(), Toast.LENGTH_SHORT).show());- Other controls: CheckBox, RadioButton in a RadioGroup, Spinner, SeekBar, ProgressBar, ImageButton.
Topic 2
Toggle buttons and images
javaToggleButton tb = findViewById(R.id.tbWifi); // or Switch / SwitchMaterial
tb.setOnCheckedChangeListener((btn, on) -> img.setImageResource(on ? R.drawable.bulb_on : R.drawable.bulb_off));
ImageView img = findViewById(R.id.imgLogo);
img.setImageResource(R.drawable.sbs_logo);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);Topic 3
Notifications and toasts
- Toast: a short message that fades automatically; Snackbar: a message with an optional action at the bottom of the screen.
- 1Create a notification channel (Android 8+)
- 2Request POST_NOTIFICATIONS permission (Android 13+)
- 3Build with NotificationCompat.Builder — icon, title, text, priority
- 4Add a PendingIntent to open an activity on tap
- 5Show with NotificationManagerCompat.notify(id, notification)
javaNotificationCompat.Builder nb = new NotificationCompat.Builder(this, "fees")
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Fee reminder")
.setContentText("Semester fee is due on 15 October")
.setAutoCancel(true);
NotificationManagerCompat.from(this).notify(1, nb.build());Topic 4
Menus and dialogs
- Options menu
- App bar items — onCreateOptionsMenu and onOptionsItemSelected
- Context menu
- Long-press on a view — registerForContextMenu
- Popup menu
- Anchored to a view, for actions related to it
javanew AlertDialog.Builder(this)
.setTitle("Delete record")
.setMessage("Are you sure?")
.setPositiveButton("Yes", (d, w) -> deleteRecord())
.setNegativeButton("No", null)
.show();- Dialog types: AlertDialog (with list, single or multiple choice), DatePickerDialog, TimePickerDialog, custom dialogs with their own layout, and DialogFragment.
Topic 5
Custom lists
- 1Design a row layout (image and two text views)
- 2Create a model class (Course)
- 3Write an adapter (ArrayAdapter or RecyclerView.Adapter) that inflates rows and binds data
- 4Attach the adapter to a ListView or RecyclerView
- 5Handle item clicks
javaString[] courses = {"BCA", "B.Sc IT", "MBA"};
ListView lv = findViewById(R.id.lvCourses);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, courses));
lv.setOnItemClickListener((p, v, pos, id) -> Toast.makeText(this, courses[pos], Toast.LENGTH_SHORT).show());- RecyclerView recycles row views with a ViewHolder and is preferred for long lists.
Topic 6
Google Maps and GPS location
- 1Add the Maps SDK dependency and an API key
- 2Add a SupportMapFragment to the layout
- 3In onMapReady add markers and move the camera
- 4Request ACCESS_FINE_LOCATION at run time
- 5Use FusedLocationProviderClient to get the last or current location
javapublic void onMapReady(GoogleMap map) {
LatLng sbs = new LatLng(30.90, 75.85); // Ludhiana (approximate)
map.addMarker(new MarkerOptions().position(sbs).title("SBS Ludhiana"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sbs, 14));
}- Location sources: GPS (accurate, outdoors, more battery), network (Wi-Fi and cell towers), fused provider (combines them efficiently).
Topic 7
Shared preferences
- Stores small key–value data (settings, login flags) in an XML file private to the app.
javaSharedPreferences sp = getSharedPreferences("settings", MODE_PRIVATE);
sp.edit().putString("user", "riya").putBoolean("darkMode", true).apply(); // save
String user = sp.getString("user", "guest"); // read with defaultTopic 8
File access
Location
App-private directory
Shared media folders, SD card
Access by other apps
No
Yes, through MediaStore or file pickers
Permission
None
Scoped storage rules from Android 10
Removed on uninstall
Yes
App-specific external files yes; shared media no
javatry (FileOutputStream fos = openFileOutput("notes.txt", MODE_PRIVATE)) {
fos.write("Lab 5 completed".getBytes());
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("notes.txt")))) {
String line = br.readLine();
}Topic 9
SQLite database (overview)
- SQLite: a lightweight, serverless, file-based relational database built into Android — ideal for structured app data such as student records or expenses. Details are in Unit 4.
Topic 10
View and drawable animation
View (tween) animation
Transforms a view: alpha, scale, translate, rotate — defined in res/anim XML
Fade in a logo
Drawable (frame) animation
Shows a sequence of images like a flip book — animation-list XML
Loading spinner
Property animation
Changes any object property over time — ObjectAnimator, ValueAnimator
Move a button and change its colour
javaAnimation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in); // view animation
logo.startAnimation(fade);
ImageView iv = findViewById(R.id.ivRun); // drawable animation
iv.setBackgroundResource(R.drawable.run_frames);
((AnimationDrawable) iv.getBackground()).start();Key terms
- Widget
- User interface control such as a button or text box
- Toast
- Brief pop-up message that disappears automatically
- Adapter
- Bridge between data and a list view
- SharedPreferences
- Key–value storage for small data
- Frame animation
- Animation showing images in sequence
Quick revision
- TextView, EditText, Button, CheckBox, RadioButton, ToggleButton, ImageView.
- Toasts, Snackbars, notification channels and builder.
- Options, context and popup menus; AlertDialog and pickers.
- Custom lists with adapters; RecyclerView; Maps SDK and fused location.
- SharedPreferences, internal and external files, SQLite; view, drawable and property animation.
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.Distinguish a toast and a notification.
- Q2.What is a notification channel?
- Q3.Distinguish options and context menus.
- Q4.What is an adapter?
- Q5.When are shared preferences used?
- Q6.Distinguish view and drawable animation.
Long-answer questions
- Q1.Explain common UI widgets with an example layout.
- Q2.Explain notifications, menus and dialogs.
- Q3.Explain custom lists and Google Maps integration.
- Q4.Explain data storage options in Android.
Stuck on this unit?
Message SBS on WhatsApp for help with Mobile Application Development, or to ask about studying B.Sc IT at Synetic.
