Week 4 User Interface Widgets
Week 4 User Interface Widgets
●
Absolute positioning (C++, C#, others):
– Programmer specifies exact pixel coordinates of every component.
– "Put this button at (x=15, y=75) and make it 70x31 px in size."
horizontal vertical
LinearLayout example 1
<LinearLayout ...
android:orientation="horizontal"
tools:context=".MainActivity">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4
Very Long Text" />
</LinearLayout>
●
In our examples, we'll use ... when omitting boilerplate
code that is auto-generated by Android Studio and not
relevant to the specific example at hand.
LinearLayout example 2
<LinearLayout ...
android:orientation="vertical"
tools:context=".MainActivity">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2
Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4
Very Long Text" />
</LinearLayout>
Gravity
●
gravity: alignment direction that widgets are pulled
– top, bottom, left, right, center
– combine multiple with |
– set gravity on the layout to adjust all widgets;
set layout_gravity on an individual widget
<LinearLayout ...
android:orientation="vertical"
android:gravity="center|right">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4 Very Long Text" />
<Button ... android:text="Button 5"
android:layout_gravity="left" />
</LinearLayout>
Weight
●
weight: gives elements relative sizes by integers
– widget with weight K gets K/total fraction of total size
– cooking analogy: "2 parts flour, 1 part water, ..."
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="B1"
android:layout_weight="1" />
<Button ... android:text="B2"
android:layout_weight="3" />
<Button ... android:text="B3"
android:layout_weight="1" />
</LinearLayout>
Widget box model
●
content: every widget or view has a certain size (width x height)
for its content, the widget itself
●
padding: you can artificially increase the widget's size by
applying padding in the widget just outside its content
●
border: outside the padding, a line around edge of widget
●
margin: separation from neighboring widgets on screen
Sizing an individual widget
●
width and height of a widget can be:
– wrap_content : exactly large enough to fit the widget's content
– match_parent : as wide or tall as 100% of the screen or layout
– a specific fixed width such as 64dp (not usually recommended)
●
dp = device pixels; dip = device-independent pixels; sp = scaling pixels
<Button ...
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Padding
●
padding: extra space inside widget
– set padding to adjust all sides;
paddingTop, Bottom, Left, Right for one side
– usually set to specific values like 10dp
(some widgets have a default value ~16dp)
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="Button 1"
android:padding="50dp" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3"
android:paddingLeft="30dp"
android:paddingBottom="40dp" />
</LinearLayout>
Margin
●
margin: extra space outside widget to separate it from others
– set layout_margin to adjust all sides;
layout_marginTop, Bottom, Left, Right
– usually set to specific values like 10dp
(set defaults in res/values/dimens.xml)
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="Button 1"
android:layout_margin="50dp" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp" />
</LinearLayout>
GridLayout
●
lays out widgets/views in lines of rows and columns
– orientation attribute defines row-major or column-major order
– introduced in Android 4; replaces older TableLayout
● by default, rows and columns are equal in size
– each widget is placed into "next" available row/column index unless it is
given an explicit layout_row and layout_column attribute
●
what layout(s) are used to create
the appearance at right?
– overall activity: __________
– internal layouts: _________
Nested layout template
<OuterLayoutType ...>
<InnerLayoutType ...>
<Widget ... />
<Widget ... />
</InnerLayoutType>
<InnerLayoutType ...>
<Widget ... />
<Widget ... />
</InnerLayoutType>
●
properties for x/y relative to layout container (the activity):
– layout_alignParentTop, Bottom, Left, Right
●
set these flags to a boolean value of "true" to enable them