Professional Documents
Culture Documents
1
Overview
All user interface elements in an Android app are built using View and
ViewGroup objects.
View is an object that draws something on the screen that the user can
interact with.
ViewGroup is an object that holds other View (and ViewGroup)
objects in order to define the layout of the interface.
Android provides a collection of both View and ViewGroup
subclasses that offer you common input controls (such as buttons and
text fields) and various layout models (such as a linear or relative
layout) 2
User interface, Widgets and Controls
User interfaces
user interface for an Android app is built using a hierarchy of layouts
(ViewGroup objects) and widgets (View objects).
Layouts are invisible containers that control how its child views are positioned
on the screen.
Widgets are UI components such as buttons and text boxes.
3
Con’t
4
Con’t
6
Con’t
• <View>
– An individual UI component, generally referred to as a "widget".
– Different kinds of View objects include TextView, Button, and
CheckBox.
• Attributes:
– android:id [unique resource name for the element ]
– android:layout_height ["match_parent" or "wrap_content“ ]
– android:layout_width ["match_parent" or "wrap_content“
7
Value
Android: id
8
Android Layouts
9
• LinearLayout
– means you can align views one by one (vertically/ horizontally).
• RelativeLayout
– means based on relation of views from its parents and other views.
• ConstraintLayout
– is similar to a RelativeLayout in that it uses relations to position and size
widgets, but has additional flexibility and is easier to use in the Layout
Editor.
• WebView: to load html, static or dynamic pages.
• FrameLayout
– to load child one above another, like cards inside a frame, we can place
one above another or anywhere inside the frame.
• AbsoluteLayout
– means you have to give exact position where the view should be.
10
Linear Layout
Linear Layout is a view group that aligns all children in a single
direction, vertically or horizontally.
You can specify the layout direction with the android:orientation
attribute.
LinearLayout respects margins between children and
the gravity (right, center, or left alignment) of each child.
11
Con’t
13
Xml Attributes of Linear layout
• android:baselineAligned
– When set to false, prevents the layout from aligning its children's
baselines.
• android:baselineAlignedChildIndex
– When a linear layout is part of another layout that is baseline aligned,
– it can specify which of its children to baseline align to (that is, which child
TextView).
• android:divider
– Drawable to use as a vertical divider between buttons.
• android:gravity
– Specifies how an object should position its content, on both the X and Y
14
Con’t
• android:measureWithLargestChild
– When set to true, all children with a weight will be considered
having the minimum size of the largest child.
• android:orientation
– Should the layout be a column or a row? Use "horizontal" for a
row, "vertical" for a column.
• android:weightSum
– Defines the maximum weight sum
15
Summary
• android:layout_gravity
– Gravity specifies how a component should be placed in its
group of cells.
• android:layout_weight
– Indicates how much of the extra space in the LinearLayout is
allocated to the view associated with these LayoutParams.
• android:divider
– Drawable to use as a vertical divider between buttons.
16
UI Elements [Widgets]
• Button
– A push-button that can be pressed, or clicked, by the user to perform an action.
• Check box
– An on/off switch that can be toggled by the user.
– You should use checkboxes when presenting users with a group of selectable
options that are not mutually exclusive.
17
Con’t
• Radio button [ RadioGroup, RadioButton ]
– Similar to checkboxes, except that only one option can be
selected in the group.
• Toggle button
– An on/off button with a light indicator.
• Spinner
– A drop-down list that allows users to select one value from a set.
• Pickers [ DatePicker, TimePicker ]
– A dialog for users to select a single value for a set by using
up/down buttons or via a swipe gesture.
– Use a <DatePickercode> widget to enter the values for the date
(month, day, year) or a TimePicker widget to enter the values for
a time (hour, minute, AM/PM). 18
Buttons
19
Con’t
With text, using the Button class:
20
Con’t
• With text and an Icon, using the Button class with the
android:drawableLeft attribute:
• Borderless button
– resemble basic buttons except that they have no borders or
background but still change appearance during different
states, such as when clicked.
– To create a borderless button, apply the
borderlessButtonStyle style to the button.
21
Con’t
• Using onClickListener
22
23
Button Styles
• style="@style/Widget.AppCompat.Button"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Widget.AppCompat.Button.Borderless"
style="@style/Widget.AppCompat.Button.Borderless.Colore
d"
24
EditText
• EditText
– A user interface element for entering and modifying text.
– When you define an edit text widget, you must specify the
TextView_inputType attribute.
– For example, for plain text input set inputType to "text":
25
Specify the Keyboard Type
• You should always declare the input method for your text fields by adding
the android:inputType attribute to the <EditText> element.
• For example,
– if you'd like an input method for entering a phone number, use the
"phone" value:
26
Con’t
• Or if the text field is for a password, use the "textPassword" value
• For example, if you want to accept a secret number, like a unique pin or serial
number, you can set inputType to "numericPassword".
– results in an edit text that accepts numbers only, shows a numeric keyboard
27
Enable Spelling Suggestions and other
Behaviors
The android:inputType attribute allows you to specify various behaviors for
the input method.
for a text message, you should enable auto spelling correction with
the "textAutoCorrect“ value.
You can combine different behaviors and input method styles with
android:inputType attribute.
For example, capitalizes the first word of a sentence and also auto-corrects
misspellings:
28
Specify the Input Method Action
29
You can then listen for presses on the action button by defining a
TextView.OnEditorActionListener for the EditText element.
In your listener, respond to the appropriate IME action ID defined in the
EditorInfo class, such as IME_ACTION_SEND.
30
Example
• EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new
OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
}); 31
32
Summary
• Fill_parent
• Wrap_content
• Textview
• Edittext
• Button
34
Check Box
• CheckBox
– is a type of two state button either checked or unchecked.
35
Con’t
<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Simple
CheckBox"/>
• Important Note: You can check the current state of a check box
programmatically by using isChecked() method.
36
Example
<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Simple
CheckBox" android:checked="true"/>
<!--set the current state of the check box-->
37
Con’t
38
Attributes of Check box
• Id
• Checked
• Gravity
– used to control the alignment of the text in CheckBox like
left, right, center, top, bottom, center_vertical,
center_horizontal etc.
39
Con’t
• Text
– used to set the text in a check box.
– We can set the text in xml as well as in the java class.
40
Con’t
• Padding
– is used to set the padding from left, right, top or bottom.
• paddingRight :
– set the padding from the right side of the check box.
• paddingLeft :
– set the padding from the left side of the check box.
• paddingTop :
– set the padding from the top side of the check box.
• paddingBottom :
– set the padding from the bottom side of the check box.
• Padding :
– set the padding from the all side’s of the check box. 41
Con’t
<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Padding Attribute Of Check Box" android:textColor="#44f"
android:textSize="20sp" android:textStyle="bold|italic"
android:checked="false" android:padding="30dp"/>
<!--30dp padding from all side’s-->
42
Lab Exercise
43
Toggle Button
44
Switch
is a two-state toggle switch widget that can select between
two options.
It is used to display checked and unchecked state of a button
providing slider control to user.
Switch is a subclass of CompoundButton.
It is basically an off/on button which indicate the current state
of Switch.
It is commonly used in selecting on/off in Sound, Bluetooth,
WiFi etc.
As compared to ToggleButton Switch provides user slider
control. The user can simply tap on a switch to change its
current state.
45
Radio Button
47
Picker
Android provides controls for the user to pick a time or pick a date
as ready-to-use dialogs.
Each picker provides controls for selecting each part of the time
(hour, minute, AM/PM) or date (month, day, year).
Using these pickers helps ensure that your users can pick a time or
date that is valid, formatted correctly, and adjusted to the user's
locale.
We recommend that you use DialogFragment to host each time or
date picker.
48
• The DialogFragment manages the dialog lifecycle for you and
allows you to display the pickers in different layout
configurations, such as in a basic dialog on handsets or as an
embedded part of the layout on large screens.
49
ToolTips
A tooltip is a small descriptive message that appears near a view
when users long press the view or hover their mouse over it.
This is useful when your app uses an icon to represent an action
or piece of information to save space in the layout.
50
Android Progress Bar
51