I3350 - Lecture 3 - App Design
I3350 - Lecture 3 - App Design
2
Everything you see is a view
3
Examples of view subclasses
Button CheckBox
EditText RadioButton
Slider Switch
4
ViewGroup and View hierarchy
• The graphical user interface for an Android app is built using a
hierarchy of View and ViewGroup objects.
• ViewGroup objects are invisible view containers that define how the
child views are laid out, such as in a grid or a vertical list.
5
ViewGroup and View hierarchy
6
ViewGroups for layouts
Layouts
• are specific types of ViewGroups (subclasses
of ViewGroup)
• contain child views
• can be in a row, column, grid, table, absolute
7
Common Layout Classes
8
View hierarchy and screen layout
9
View hierarchy in the layout editor
10
Create views and layouts
3. Java code
11
Android Studio layout editor
12
Layout created in XML
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
... />
<TextView
... />
<Button
... />
</LinearLayout
13
Best practices for view hierarchies
14
TextView and EditText
15
TextView for text
• TextView is View subclass for single and multi-line text
• EditText is TextView subclass with editable text
• Controlled with layout attributes
• Set text:
– Statically from string resource in XML
– Dynamically from Java code and any source
16
Creating TextView in XML
<TextView android:id="@+id/textView_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_message"/>
17
Common TextView attributes
android:text—text to display
android:textColor—color of text
android:textAppearance—predefined style or theme
android:textSize—text size in sp
android:textStyle—normal, bold, italic, or bold|italic
android:typeface—normal, sans, serif, or monospace
android:lineSpacingExtra—extra space between lines in sp
18
android:id attribute
android:id="@+id/textView_message"
• This provides a unique identifier for the view, which you can use to reference the
object from your app code, such as to read and manipulate the object.
• The at sign (@) is required when you're referring to any resource object from XML.
It is followed by the resource type (id in this case), a slash, then the resource name
(textView_message). (Note that textView_message is a user chosen name).
• The plus sign (+) before the resource type is needed only when you're defining a
resource ID for the first time.
19
android:layout_width & android:layout_height
• The user can provide specific values for attributes android:layout_width and
android:layout_height.
• Android supports the following measurements:
– px (Pixels) - Actual pixels or dots on the screen.
– in (Inches) - Physical size of the screen in inches.
– mm (Millimeters) - Physical size of the screen in millimeters.
– pt (Points) - 1/72 of an inch.
– dp (Density-independent Pixels): density-independent
pixels (also
known as device-independent-pixels) help to keep UI elements
the same physical size across the different screen density
devices.
• Example: android:layout_width = “20dp”
20
android:layout_width & android:layout_height
• Instead of using specific sizes for the width and height, the "wrap_content" value
specifies that the view should be only as big as needed to fit the contents of the
view.
• If you were to instead use "match_parent", then the view would fill its parent.
21
Adding String Resources
• When you need to add text in the user interface, you should always specify
each string as a resource.
• String resources allow you to manage all UI text in a single location, which
makes it easier to find and update text.
• Externalizing the strings also allows you to localize your app to different
languages by providing alternative definitions for each string resource.
• Add a new string named "welcome_message" and set the value to "Welcome
to CENG435." ( note that "welcome_message" is the string variable name
while "Welcome to CENG435." is the content that will displayed on the screen).
22
String.xml file
</resources>
Variable name value
23
Creating EditText in XML
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message_hint" />
24
Toasts
25
Toasts
• A toast provides simple feedback about an
operation in a small popup.
26
Toasts
• To create a Toast, here is an example:
27
Event Handling
28
Events
29
Event Handlers
30
Event handler for button click
Let’s add a button to the activity layout:
• <Button anroid:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text" />
31
Attach in XML and implement in
Java
Attach handler to view in XML layout: Implement handler in Java activity:
32
Attach in XML and implement in Java
The button declaration code in XML becomes:
• <Button anroid:id="@+id/button_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:onClick="addValues" />
33
Alternative: Set click handler in
Java
Button button = (Button) findViewById(R.id.button_toast);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// code that adds values and shows result…
}
});
34
Resources and measurements
35
Resources
36
Where are the resources in your
project?
37
Refer to resources in code
• Layout:
R.layout.activity_main
setContentView(R.layout.activity_main);
• View:
R.id.recyclerView
rv = (RecyclerView) findViewById(R.id.recyclerView);
• String:
In Java: R.string.title
In XML: android:text="@string/title"
38
Measurements
39