Professional Documents
Culture Documents
L02 - Android Layout (ITP4501) 2020
L02 - Android Layout (ITP4501) 2020
L02 - Android Layout (ITP4501) 2020
Android Layouts 1
Android - Fragmentation
http://opensignal.com/reports/fragmentation-2013/
Android Layouts 2
Android UI Design Challenges
Android Layouts 3
Flexible UI Design
Android Layouts 4
Android User Interface
Android Layouts 5
Android User Interface
Android Layouts 6
User Interface
Android Layouts 7
View Hierarchy
Can be a
TextView
(Label) object Can be a Button
widget
Picture Source - https://stackoverflow.com/tags/android-layout/info Android Layouts 8
Layout
You can build android UI in two ways:
• Declare UI elements in XML format
Android provides a set of XML tag, that
allows you to define application's default
layouts in XML, including the screen
elements that will appear in them and their
properties.
• Instantiate layout elements at runtime Your
application can create View and ViewGroup
objects (and manipulate their properties)
programmatically.
Android Layouts 9
XML-based Layout
Android Layouts 11
XML-based Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
Sample
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=“match_parent"
android:layout_height=“match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
Android Layouts 13
Attributes
The attributes of the XML elements are
properties, describing how a widget should
look or how a container should behave.
ID - Any View object may have an integer ID
associated with it, to uniquely identify the
View within the tree.
Example:
◦ A button is defined in the layout file and assigned a
unique ID.
android:id="@+id/my_button"
Android Layouts 14
Attributes
Layout Parameters - the XML layout
attributes named layout_something contains
property types that define the size and
position for each child view, as appropriate
for the view group.
The parent view group defines layout parameters for each child
view (including the child view group).
https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/ui/declaring-layout.html
Android Layouts 15
Common Layouts
Android Layouts 16
FrameLayout
FrameLayout is an area on the screen to display
a single item.
You can add multiple children to a
FrameLayout, but all children are pinned to the
top left corner of the screen. You cannot specify
a different location for a child view.
Adding multiple views to a frame layout just
stacks one on top of the other (overlapping the
views).
Android Layouts 17
LinearLayout
Android Layouts 18
LinearLayout
Android Layouts 19
Orientation in LinearLayout
Vertical
Android Layouts 20
Orientation in LinearLayout
Or, add the android:orientation property to your
LinearLayout element in your XML layout, setting
the value to be horizontal / vertical.
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width=“match_parent"
android:layout_height="match_parent"
android:background="#ff0033cc"
android:padding="4dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" >
<TextView …></TextView>
<EditText …></EditText>
<Button …></Button>
</LinearLayout>
Android Layouts 21
Fill Model in LinearLayout
Widgetshave a "natural" size based on their
accompanying text.
Natural sizes
Empty screen space
Android Layouts 22
Fill Model in LinearLayout
Android Layouts 23
Fill Model in LinearLayout
Android Layouts 24
Fill Model in LinearLayout
325dip
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …
125dip
android:orientation="vertical"
…
>
<TextView android:id="@+id/labelUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
… ></TextView>
<EditText android:id="@+id/ediName"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
… ></EditText>
<Button android:id="@+id/btnGo"
android:layout_width="125dip"
android:layout_height="wrap_content"
… ></Button>
</LinearLayout>
Specific size: 125 dip
Android Layouts 25
Weight in LinearLayout
Android Layouts 26
Weight in LinearLayout
TextView:
android:layout_weight=“2"
EditText:
android:layout_weight=“3"
Button:
android:layout_weight=“1"
android:layout_gravity = “right”
Android Layouts 28
Gravity in LinearLayout
gravity vs layout_gravity
◦ android:gravity - specifies how to place the
content of an object, both on the x-and y-axis,
within the object itself.
Android Layouts 29
Padding in LinearLayout
Android Layouts 30
Padding in LinearLayout
Padding Top
View’s content
Padding Bottom
Margin Bottom
Android Layouts 31
Padding in LinearLayout
For example, the following EditTextbox has been
changed to display 30dip of padding all round.
30dip <EditText
android:id="@+id/ediName"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="30dip"
>
</EditText>
...
Android Layouts 32
Padding in LinearLayout
By default, widgets are tightly packed next to each
other. To increase space between them use the
android:layout_margin attribute.
<EditText
android:id="@+id/ediName"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
Increased the inter-widget
space android:layout_margin=“6dip“
>
</EditText>
...
Android Layouts 33
AbsoluteLayout
Y=98dip
X=76dip
Android Layouts 34
AbsoluteLayout
<?xml version="1.0" encoding="utf-8"?> <EditText
<AbsoluteLayout android:id="@+id/etName"
android:id="@+id/myLinearLayout" android:layout_width=“match_parent"
android:layout_width=“match_parent" android:layout_height="wrap_content"
android:layout_height=“match_parent" android:textSize="18sp"
android:background="#ff0033cc" android:layout_x="0dip"
android:padding="4dip" android:layout_y="38dip“ >
xmlns:android="http:// </EditText>
schemas.android.com/apk/res/android"
> <Button
<TextView android:layout_width="120dip"
android:id="@+id/tvUserName" android:text="Go"
android:layout_width=“match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#ffff0066" android:id="@+id/btnGo"
android:text="User Name" android:layout_x="100dip"
android:textSize="16sp" android:layout_y="170dip" />
android:textStyle="bold"
android:textColor="#ff000000" </AbsoluteLayout>
android:layout_x="0dip"
android:layout_y="10dip“ >
</TextView>
Android Layouts 37
Example of ScrollView
<?xml version="1.0" encoding="utf-8"?> <TextView
<ScrollView android:id="@+id/textView3"
android:id="@+id/myScrollView1" android:layout_width=“match_parent"
android:layout_width=“match_parent" android:layout_height="wrap_content"
android:layout_height=“match_parent" android:background="#ffccffcc"
android:background="#ff009999" android:text="Line3"
> android:textSize="70dip"/>
<LinearLayout <TextView
android:layout_width="match_parent" android:id="@+id/textView4"
android:layout_height="match_parent" … />
android:orientation="vertical" >
<TextView
<TextView android:id="@+id/textView5"
android:id="@+id/textView1" … />
android:layout_width=“match_parent"
android:layout_height="wrap_content" <TextView
android:background="#ffccffcc" android:id="@+id/textView6"
android:text="Line1" … />
android:textSize="70dip"/>
<TextView
<TextView android:id="@+id/textView7"
android:id="@+id/textView2" … />
android:layout_width=“match_parent"
android:layout_height="wrap_content" </LinearLayout>
android:text="Line2" </ScrollView>
android:textSize="70dip"/>
Android Layouts 38
Other example
Android Layouts 39
Devices and Displays
Android Layouts 40
• There are Android devices with a wide variety of
screen sizes and resolutions.
• Be flexible – Stretch and compress your layouts to
accommodate various heights and widths.
• Optimize layouts – On large devices, create compound
views that combine multiple views to reveal more content
and ease navigation.
• Work for all – Handle devices with different screen
densities (DPI) to ensure that the app looks great on any
device.
Android Layouts 41
Supporting multiple screens
• Screen size – Actual physical size, measured in
screen’s diagonal.
• Four generalized sizes: small, normal, large, xlarge.
• Screen density – The quantity of pixels within a
physical area of the screen, usually referred to as
dpi (dots per inch).
• Four generalized densities: low (ldpi), medium (mdpi), high
(hdpi), extra high (xhdpi).
Update: xxhdpi
(~480dpi) is added
in 2013!
Android Layouts 42
Supporting multiple screens
• To optimize user experience on different screen sizes, you
can create a unique layout XML file for each screen size you
want to support.
• Each layout should be saved into the appropriate resources
directory, named with a -<screen_size> suffix.
• For example, a unique layout for large screens should be
saved under res/layout-large/.
MyProject/ The file names must be
res/ exactly the same, but their
layout/ contents are different in order
main.xml to provide an optimized UI for
layout-large/ the corresponding screen
main.xml size.
• The system loads the layout file from the appropriate layout
directory based on screen size of the device on which your
app is running.
Android Layouts 43
Supporting multiple screens
• As another example, here's a project with an alternative
layout for landscape orientation:
MyProject/
res/
By default, the
layout/
layout/main.xml file
main.xml
is used for portrait
layout-land/
orientation.
main.xml
Landscape orientation
Portrait orientation
Android Layouts 44
Create Landscape in Android Studio
• Create the Portrait Layout first (default layout)
• And then under the icon menu bar of Layout,
choose orientation button and then select "Create
Landscape Variant"
• You will find you have got one more layout with
the word "(land)"
Android Layouts 45
Density independence
• Density-Independent Pixel (dp)
• A virtual pixel unit that you should use when defining UI
layout, to express layout dimensions or position in a
density-independent way.
• The dp is equivalent to one physical pixel on a 160 dpi
screen, which is the baseline density assumed for a
“medium” density screen.
• At runtime, Android handles any scaling of the dp units
based on the actual density of the screen in use.
• Conversion: px = dp * (dpi / 160)
• For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels.
Android Layouts 46
Density independence
• Maintaining density independence is important
because, without it, a UI element (such as button)
appears physically larger on a low density screen
and smaller on a high density screen.
Without density
independence
With density
independence
Android Layouts 47
How to support multiple screens
• Explicitly declare in the manifest which screen sizes
your application supports.
android:smallScreens=["true" | "false"]
android:normalScreens=["true" | "false"]
android:largeScreens=["true" | "false"]
android:xlargeScreens=["true" | "false"]
android:anyDensity=["true" | "false"]
android:requiresSmallestWidthDp="integer"
android:compatibleWidthLimitDp="integer"
android:largestWidthLimitDp="integer"/>
Android Layouts 48
• Provide different layouts for different
screen sizes
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml / layout for extra large in landscape orientation
Android Layouts 49
• Provide different bitmap drawables for different
screen densities
• By default, Android scales bitmap drawables (png, gif, jpg)
and 9-Patch drawables (.9.png) so that they render at the
appropriate physical size on each device.
• However, to ensure bitmaps look their best, you should
include alternative versions at different resolutions for
different screen densities.
• res/drawable-hdpi
• res/drawable-mdpi
• res/drawable-ldpi
Android Layouts 50
Support different languages
• To add support for more languages, create
additional values directories inside res/ that
include a hyphen and the ISO country code at the
end of the directory.
• Android loads the appropriate resources according
to the locale settings of the device at run time.
MyProject/
res/
values/
strings.xml
values-fr/
strings.xml
Android Layouts 51
Support different languages
• English (default locale), /values/strings.xml:
• French, /values-fr/strings.xml:
Android Layouts 52
Create Multi Language (Locale) in Android Studio
Under the project tree right click on "res" and then select New,
Android Resource Directory.
Choose "Locale" and then click the ">>" button.
Choose the country and corresponding region.
Android Layouts 53
Create Multi Language (Locale) in Android Studio
Android Layouts 54
Create Multi Language (Locale) in Android Studio
Android Layouts 55
More about Android Studio
• You can apply a three month trial on
the famous online training by using
IVE student email with CNA password.
◦ https://docs.microsoft.com/en-us/visualstu
dio/subscriptions/vs-pluralsight
• And then go to the following tutorial in
pluralsight:
◦ https://app.pluralsight.com/library/courses/
android-start-developing/table-of-contents
• No coding, only on usage of Android
Studio, Layout, and xml files
Android Layouts 56