Week_2_Basic_Application
Week_2_Basic_Application
Creating
Application
CET3013: MOBILE APPLICATION DEVELOPMENT
Today’s Topics
An Android Application?
Activities
Layouts
UI design and views
Main Template Launch Code
This file is in MainActivity.kt
What's an Activity?
Where's the main method?
Where does it say to print "Hello
World!” ?
Views, ViewGroups & Layouts
(FINAL)
A View is the Android term for a user-interface element.
- We’ll see more about Views later.
A ViewGroup is a special kind of View that can have one or
more children.
Layouts are subclasses of ViewGroup.
Most recommended: Constraint layout – can be used also
to do nested layout
Other layout will take more memory
Layouts TableLayout, TableRow, Space normally didn’t use
No matter how many children there are, there will only
ever be one row or column, no matter the sizes of the
individual views.
It respects, for its children:
◦ Margins
◦ Gravity
◦ Right, Centre or Left Alignment
LinearLayout
We can assign a weight to individual children.
Determines how much space they take up.
Example:
◦ Three buttons, weights 0, 2, and 1
◦ The first button only takes up the space required by its content.
◦ The second and third buttons take up the remaining space.
RelativeLayout
Child positions are specified relative to other children, using
their IDs.
Flexibile, powerful but rather more complex.
GridBagLayout in Swing (ever used it?)
RelativeLayout Example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue"
android:padding="10px" >
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="OK" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
Constraint Layouts
New in Studio 2.2!
ConstraintLayout is a view group available in the Constraint
Layout library,
◦ Claimed should never need to edit the XML by hand.
Source: https://workingdev.net/java/android/2020/04/23/event-handling-android.html
Principal Event Handlers
“View” is a parameter which is GUI itselfs
Listener Details Public Method to implement
onClickListener Responds to a click void onClick(View v)
onLongClickListener Responds to a long boolean onLongClick(View v)
click
onKeyListener Respond to a Key boolean onKey(View v, int keyCode,
Event KeyEvent event)
onTouchListener Respond to a touch boolean onTouch(View v, MotionEvent
event)
Methods which return a boolean should return true if the event has been
“consumed”.
There are a couple of others—and several additional ones for later versions of
Android.
Types of View
TextView ViewFlipper
EditText QuickContactBadge
ListView AnalogClock, DigitalClock
Spinner KeyboardView
Button ProgressBar
CheckBox DatePicker, TimePicker
RadioButton ScrollView
RadioGroup WebView
There are several others too—some of which we will meet the labs.
EditText
This example uses an EditText to enable the user to enter a
string of text.
When Return is pressed, the string is displayed using a
Toast.
EditText
Lambda function – no need the () and rewrite the parameter inside
CheckBox
RadioButtons—Layout
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="@+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
RadioButtons—Code
//TODO CODE:
Alert Dialogs
Creating a dialog
◦ All of AlertDialog’s constructors are protected.
◦ What to do?
◦ Use AlertDialog.Builder.
◦ Should be created in an override of the Activity method
onCreateDialog(int).
Showing a dialog
◦ Call showDialog(int), passing the ID of the dialog you
want to show.
Two common button:
OK means positive button
Cancel means negative button
Alert Dialogs
Alert Dialogs
More code here soon
Alert Dialogs
Displaying Dialog
Resolution and Density
Independence
Unlike (say) iDevices, there is a very wide range of
Android devices with different capabilities.
We want our applications to run unmodified on as
many as possible (why?)
Screen sizes from 3in to 10in
Different resolutions
Android offers support!
Resolution and Density
Independence
Makes use of the optional sequences to resource files.
◦ Screen size: small, medium, large
◦ Resolution
◦ Ldpi – 120px (New android studio no longer support this small densities already)
◦ Mdpi – 160px
◦ Hdpi – 240px
◦ Xhdpi – 320px
Resolution and Density
Independence
Different size of screen will automatically use different size of icon,
so need to prepare various size of the icon
If not the app will be crashed when use only 1 icon to fulfil every size of android