Upload
others
View
5
Download
1
Embed Size (px)
Citation preview
1
Kode ISH3J4 – Pemrograman Aplikasi Bergerak
Pemrograman MobilePertemuan II
Rahmat Fauzi, S.T.,M.T
Sistem Informasi – Fakultas Rekayasa Industri
2
2
Sprint 1
3MINGGU LALU: MENCOBA DEBUG DI HP SENDIRI
4
1.2 Layouts and resources for the UI
4
5Contents
5
VIEWS, VIEW GROUPS, AND VIEW HIERARCHY
THE LAYOUT EDITOR AND CONSTRAINTLAYOUT
EVENT HANDLING RESOURCES AND MEASUREMENTS
6
6
ViewsIf you look at your mobile device, every user interface element that you see is a View.
Everything you see is a view
7
7
What is a view?
View subclasses are basic user interface building blocks :
● Display text (TextView class), edit text (EditText class)● Buttons (Button class), menus, other controls● Scrollable (ScrollView, RecyclerView)● Show images (ImageView)● Group views (ConstraintLayout and LinearLayout)
8
8
Button
EditText
Slider
CheckBox
RadioButton
Switch
Examples of view subclasses
9
9
● Color, dimensions, positioning● May have focus (e.g., selected to receive user input)● May be interactive (respond to user clicks)● May be visible or not● Relationships to other views
View attributes
10
10
ViewGroup and View hierarchy
11
11
ViewGroup contains "child" views
● ConstraintLayout: Positions UI elements using constraint connections to other elements and to the layout edges
● ScrollView: Contains one element and enables scrolling ● RecyclerView: Contains a list of elements and enables scrolling by
adding and removing elements dynamically
12
12
ViewGroups for layouts
Layouts ● are specific types of ViewGroups (subclasses of ViewGroup)● contain child views● can be in a row, column, grid, table, absolute
13Common Layout Classes
13
LinearLayout ConstraintLayout GridLayout TableLayout
14Common Layout Classes
● ConstraintLayout: Connect views with constraints ● LinearLayout: Horizontal or vertical row● RelativeLayout: Child views relative to each other● TableLayout: Rows and columns● FrameLayout: Shows one child of a stack of children
14
15
15
View hierarchy and screen layout
16
16
The layout editor and ConstraintLayout
17The layout editor with ConstraintLayout
17
● Connect UI elements to parent layout
● Resize and position elements
● Align elements to others
● Adjust margins and dimensions
● Change attributes
18What is ConstraintLayout?
18
● Default layout for new Android Studio project
● ViewGroup that offers flexibility for layout design
● Provides constraints to determine positions and alignment of UI elements
● Constraint is a connection to another view, parent layout, or invisible guideline
19Layout editor main toolbar
19
1. Select Design Surface: Design and Blueprint panes2. Orientation in Editor: Portrait and Landscape3. Device in Editor: Choose device for preview4. API Version in Editor: Choose API for preview5. Theme in Editor: Choose theme for preview 6. Locale in Editor: Choose language/locale for preview
20ConstraintLayout toolbar in layout editor
20
1. Show: Show Constraints and Show Margins2. Autoconnect: Enable or disable3. Clear All Constraints: Clear all constraints in layout4. Infer Constraints: Create constraints by inference5. Default Margins: Set default margins6. Pack: Pack or expand selected elements7. Align: Align selected elements8. Guidelines: Add vertical or horizontal guidelines9. Zoom controls: Zoom in or out
21Autoconnect
21
● Enable Autoconnect in toolbar if disabled
● Drag element to any part of a layout
● Autoconnect generates constraints against parent layout
22ConstraintLayout handles
22
1. Resizing handle2. Constraint line and handle3. Constraint handle4. Baseline handle
23Align elements by baseline
23
1. Click the baseline constraint button
2. Drag from baseline to other element's baseline
24Attributes pane
24
● Click the Attributes tab● Attributes pane includes:
○ Margin controls for positioning
○ Attributes such as layout_width
25Attributes pane view inspector
25
1. Vertical view size control specifies layout_height
2. Horizontal view size control specifies layout_width
3. Attributes pane close button
26Set attributes example: TextView
26
27Preview layouts
27
Preview layout with horizontal/vertical orientation:
1. Click Orientation in Editor button2. Choose Switch to Landscape or Switch to Portrait
Preview layout with different devices:
1. Click Device in Editor button 2. Choose device
28Create layout variant for landscape
28
1. Click Orientation in Editor button
2. Choose Create Landscape Variation
3. Layout variant created: activity_main.xml (land)
4. Edit the layout variant as needed
29Create layout variant for tablet
29
1. Click Orientation in Layout Editor
2. Choose Create layout x-large Variation
3. Layout variant created: activity_main.xml (xlarge)
4. Edit the layout variant as needed
30
30
Event Handling
31Events
31
Something that happens
● In UI: Click, tap, drag
● Device: DetectedActivity such as walking, driving, tilting
● Events are "noticed" by the Android system
32Event Handlers
32
Methods that do something in response to a click
● A method, called an event handler, is triggered by a specific event and does something in response to the event
33Attach in XML and implement in Java
33
Attach handler to view in XML layout:android:onClick="showToast"
Implement handler in Java activity:public void showToast(View view) {
String msg = "Hello Toast!";Toast toast = Toast.makeText(
this, msg, duration);toast.show();}
}
34Alternative: Set click handler in Java
34
final Button button = (Button) findViewById(R.id.button_id);button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {String msg = "Hello Toast!";Toast toast = Toast.makeText(this, msg, duration);toast.show();}
});
35
35
Resources and measurements
36Resources
● Separate static data from code in your layouts.
● Strings, dimensions, images, menu text, colors, styles
● Useful for localization
36
37Where are the resources in your project?
37
resources and resource filesstored in res folder
38Values Folder
38
Name File DescriptionColor res/values/colors.xml Define colorDimensions res/values/dimens.xml Define size i.g. padding,
margin, font, etc.Strings res/values/strings.xml Define textStyles res/values/styles.xml Define Style
39Refer to resources in code
39
● 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.titleIn XML: android:text="@string/title"
40Measurements
● Density-independent Pixels (dp): for Views
● Scale-independent Pixels (sp): for text
Don't use device-dependent or density-dependent units:
● Actual Pixels (px)
● Actual Measurement (in, mm)
● Points - typography 1/72 inch (pt)
40
41
41
Textview
42TextView 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
42
43Formatting text in string resource
● Use <b> and <i> HTML tags for bold and italics
● All other HTML tags are ignored
● String resources: one unbroken line = one paragraph
● \n starts a new a line or paragraph
● Escape apostrophes and quotes with backslash (\", \')
● Escape any non-ASCII characters with backslash (\)
43
44Creating TextView in XML
<TextView android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_story"/>
44
45Common TextView attributes
android:text—text to displayandroid:textColor—color of textandroid:textAppearance—predefined style or themeandroid:textSize—text size in spandroid:textStyle—normal, bold, italic, or bold|italicandroid:typeface—normal, sans, serif, or monospaceandroid:lineSpacingExtra—extra space between lines in sp
45
46Formatting active web links
<string name="article_text">... www.rockument.com ...</string>
<TextViewandroid:id="@+id/article"android:layout_width="wrap_content"android:layout_height="wrap_content"android:autoLink="web"android:text="@string/article_text"/>
46
Don’t use HTML for a web link in free-form text
autoLink values:"web", "email", "phone", "map", "all"
47Creating TextView in Java code
TextView myTextview = new TextView(this);
myTextView.setWidth(LayoutParams.MATCH_PARENT);
myTextView.setHeight(LayoutParams.WRAP_CONTENT);
myTextView.setMinLines(3);
myTextView.setText(R.string.my_story);
myTextView.append(userComment);
47
48
48
ScrollView
49What about large amounts of text?
● News stories, articles, etc…● To scroll a TextView, embed it in a ScrollView● Only one View element (usually TextView) allowed in a
ScrollView● To scroll multiple elements, use one ViewGroup (such as
LinearLayout) within the ScrollView
49
50ScrollView for scrolling content
● ScrollView is a subclass of FrameLayout● Holds all content in memory● Not good for long texts, complex layouts● Do not nest multiple scrolling views● Use HorizontalScrollView for horizontal scrolling● Use a RecyclerView for lists
50
51
<ScrollViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/article_subheading">
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content".../>
</ScrollView>
ScrollView layout with one TextView
51
52
<ScrollView ...<LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical">
<TextViewandroid:id="@+id/article_subheading" .../>
<TextViewandroid:id="@+id/article" ... />
</LinearLayout></ScrollView>
ScrollView layout with a view group
52
53ScrollView with image and button
<ScrollView...>
<LinearLayout...>
<ImageView.../>
<Button.../>
<TextView.../>
</LinearLayout>
</ScrollView>53
Children of the layout
One child of ScrollViewwhich can be a layout
54
54
Hallo Toast
55
SI4108 :bit.ly/Video1SI4108
(Terakhir 23.59 Selasa, 28 Januari 2020)
55
Pengumpulan Praktikum
56
Thank YouAny Question ? ?
57
Daftar Referensi
1) https://developer.android.com/courses/fundamentals-training/toc-v2