CET 343 Android Mobile
Development
Session 7
More UI components
Recap
ConstraintLayout
LinearLayout
FrameLayout
TableLayout
RelativeLayout
Agenda
●
Components: Check Boxes, Radio Buttons, Spinners, Toast, Pop up
Dialog.
The Spinner
●
●
The Spinner can be useful for when you have limited UI space and don’t
have enough room for a RadioGroup
Another advantage of a Spinner is that it restricts user input and can be
useful for validation
Converting the tip calculator
1.
Update the strings resource file so that we have an array of possible tip percentages
<string-array name="tip_options">
<item>Ten Percent</item>
<item>Fifteen Percent</item>
<item>Twenty Percent</item>
</string-array>
<string name = "tip_prompt">Select your tip percentage</string
Activity logic
Activity logic
Activity logic
Array adapters
Trend for UI components
●
●
●
●
●
Create UI
Make IDs
Declare class level variables in activity
Instantiate those variables in onCreate()
Implement the relevant listener and activity logic.
○
○
Or state the onClick event as an XML attribute
Or create an Inner Class
UI component summary
●
●
●
●
Layouts
Checkboxes
Radio Buttons
Spinners.
○
●
There are lots more options, check out the android studio developer tutorials.
No consideration of input validation at present…
Toast
●
●
●
●
●
Allows a runtime user notification
Don’t want to waste screen space on something temporary
Appears as a burst of text that quickly fades away without user
intervention
Never receives focus
Can be LENGTH_LONG or LENGTH_SHORT
Toast example
Toast.makeText(this, "The maximum number
of coffees you can order is 100",
Toast.LENGTH_SHORT).show();
Reusable toast example
Instantiate a Toast object with makeText() method.
Then display it using show()
Positioning with Toast
Default location is the bottom centre of the screen.
Can be changed with setGravity(int gravity, int x-offset, int y-offset)
Top Left Corner:
Custom Toast
https://developer.android.com/guide/topics/ui/notifiers/toasts
Tip Calculator with Toast
Tip Calculator with Toast
AlertDialog
●
●
●
Pop up that requests a response from the user.
Requires an AlertDialog.Builder to configure the AlertDialog.
Can show a title, up to 3 buttons, a list of selectable items, or a custom
layout.
AlertDialog
Class: AlertDialog
Package: android.app
Extends: android.app.Dialog
Description: A subclass of dialog that can contain one, two or three buttons. A string can be displayed
using the setMessage() method.
Methods:
void setTitle(CharSequence title)
void setMessage(CharSequence message)
void setView(View view)
Button getButton(int whichButton)
ListView getListView()
To build an AlertDialog
Basic example
The set...Button() methods require a title for the button (supplied by a
string resource) and a DialogInterface.OnClickListener that defines
the action to take when the user presses the button.
There are three different action buttons you can add:
Positive: You should use this to accept and continue with the action
(the "OK" action).
Negative: You should use this to cancel the action.
Neutral: You should use this when the user may not want to proceed
with the action, but doesn't necessarily want to cancel. It appears
between the positive and negative buttons. For example, the action
might be "Remind me later."
Alert Dialog - adding a list
There are three kinds of lists available with the AlertDialog APIs
●
●
●
A traditional single-choice list
A persistent single-choice list (radio buttons)
A persistent multiple-choice list (checkboxes)
To create a single-choice list you use setItems() method.
Alert Dialog - adding a list
Create string array resource to represent the items you
want user to select from
Application logic
Things we can do with AlertDialog
Use the setMultiChoiceItems() or setSingleChoiceItems() methods.
Implement Positive and Negative buttons to allow user to confirm or cancel
the operation.
Create an ArrayList() member variable to store the items which the user
has selected (for multi choice actions)