Professional Documents
Culture Documents
Tzu-Yang (Ben) Yu
1
Announcement
• Assignment 1 is out.
2
Today's Content
3
Create an Android project
4
MainActivity and Main Layout
5
MainActivity and Main Layout (explain)
package name
import libraries
6
What is extends? (Inheritance)
Inheritance is a mechanism where a class (subclass) acquires the properties (e.g. methods and fields) of another class
(super class).
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the
keyword super. Consider this class, Superclass:
7
The Main Layout
8
The Main Layout
9
The Main Layout
Text view ID
Button ID
11
Add a Button Using the Design Editor
Find the Button component in the palette, click on it, and then drag it into the
design editor so that it’s positioned above the text view. The button appears in
the layout’s design:
12
Layout Editor showing a layout file in Design mode
1. Palette - Displays the widgets and
layouts that may be dragged into the
Design Editor.
2. Component Tree - Displays the view
hierarchy for the layout.
3. Toolbar - Buttons to configure the
layout's appearance in the Design
Editor.
4. Design Editor - Displays the layout in
Design view (how the layout appears
on a device) and Blueprint view
(outlines of each View).
5. Attributes - For editing the attributes
of the currently selected widget or
layout.
13
Question
Question?
Before we move to the next topic..
14
Linking Element and Adding Listener
We can add button click event listener in two ways:
Option 1: Add OnClickListener()
15
Linking Element and Adding Listener
Option 2: Create a click method and linked with ‘android:onClick’
16
R.java
• R.java is a special Java file that gets generated by Android Studio whenever
you create a view object with an ID “@+id/labels”.
• The R file is used to keep track of the resources used within the app and it
enables you to retrieve references to resources in your app.
• Note: You never change any of the code within the R file
17
Use String Resource
• To organize your text display, we can use the String resource.
• You can find the String.xml under res\values.
18
Spinner
Infer constraints
19
Add Items to Spinner
Option 1: using ‘setAdapter()’
20
Add Items to Spinner
Option 2: Create a click method and linked with ‘android:onClick’
21
Question
Question?
Before we move to the next topic..
22
Model View Controller-Exercise
23
Any Questions?
Q Any Questions?
24