Mobile App Development Chapter Twooo
Mobile App Development Chapter Twooo
• The basic building block for user interface is a View object which is created from
the View class and occupies a rectangular area on the screen and is responsible for
drawing and event handling.
• View is the base class for widgets, which are used to create interactive UI
components like buttons, text fields, etc.
• The ViewGroup is a subclass of View and provides invisible container that hold
other Views or other ViewGroups and define their layout properties.
• A View is an object that draws something on the screen that the user can interact
with and a ViewGroup is an object that holds other View (and ViewGroup)
objects in order to define the layout of the user interface.
• A layout may contain any type of widgets such as buttons, labels, textboxes, and
so on. Following is a simple example of XML file having LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Button" />
<!-- More GUI components go here -->
</LinearLayout>
• Once your layout has created, you can load the layout resource from
your application code, in your Activity.onCreate() callback
implementation as shown below
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Android Layout Types
• There are number of Layouts provided by Android which you will use in almost all the
Android applications to provide different view, look and feel.
• Linear Layout: LinearLayout is a view group that aligns all children in a single
direction, vertically or horizontally.
• Relative Layout: RelativeLayout is a view group that displays child views in relative
positions.
• Table Layout: TableLayout is a view that groups views into rows and columns.
• Absolute Layout: AbsoluteLayout enables you to specify the exact location of its
children.
• Frame Layout: The FrameLayout is a placeholder on screen that you can use to display
a single view.
• List View: ListView is a view group that displays a list of scrollable items.
• Grid View: GridView is a ViewGroup that displays items in a two-dimensional,
scrollable grid
Layout Attributes
• Each layout has a set of attributes which define the visual properties of that layout.
• android:id: This is the ID which uniquely identifies the view.
• android:layout_width: This is the width of the layout.
• android:layout_height: This is the height of the layout
• android:layout_marginTop: This is the extra space on the top side of the layout.
• android:layout_marginBottom: This is the extra space on the bottom side of the layout.
• android:layout_marginRight: This is the extra space on the right side of the layout.
• android:paddingLeft: This is the left padding filled for the layout.
• width and height are the dimension of the layout/view which can be specified in terms of
dp (Density-independent Pixels), sp ( Scale-independent Pixels), pt ( Points which is 1/72
of an inch), px( Pixels), mm ( Millimeters) and finally in (inches).
• android:layout_marginRight: This is the extra space on the right side of the layout.
• android:paddingLeft: This is the left padding filled for the layout.
• width and height are the dimension of the layout/view which can be specified in terms
of dp (Density-independent Pixels), sp ( Scale-independent Pixels), pt ( Points which
is 1/72 of an inch), px( Pixels), mm ( Millimeters) and finally in (inches).
• You can specify width and height with exact measurements but more often, you will
use one of these constants to set the width or height −
• android:layout_width=wrap_content tells your view to size itself to the dimensions
required by its content.
• android:layout_width=fill_parent tells your view to become as big as its parent
view.
View Identification
• A view object may have a unique ID assigned to it which will identify the View
uniquely within the tree. The syntax for an ID, inside an XML tag is
android:id="@+id/my_button“
• The at-symbol (@) at the beginning of the string indicates that the XML parser
should parse and expand the rest of the ID string and identify it as an ID resource.
• The plus-symbol (+) means that this is a new resource name that must be created
and added to our resources.
• To create an instance of the view object and capture it from the layout, use the
following
• Button myButton = (Button) findViewById(R.id.my_button);
Android - UI Controls
• Input controls are the interactive components in your app's user interface. Android
provides a wide variety of controls you can use in your UI, such as buttons, text
fields, seek bars, check box, zoom buttons, toggle buttons, and many more.
UI Elements
Android UI Controls
▪ TextView: This control is used to display text to the user.
▪ EditText: EditText is a predefined subclass of TextView that includes rich editing
capabilities.
▪ AutoCompleteTextView: The AutoCompleteTextView is a view that is similar to
EditText, except that it shows a list of completion suggestions automatically while
the user is typing.
• Button : A push-button that can be pressed, or clicked, by the user to perform an
action.
• ImageButton: An ImageButton is an AbsoluteLayout which enables you to specify
the exact location of its children. This shows a button with an image (instead of
text) that can be pressed or clicked by the user.
• CheckBox: An on/off switch that can be toggled by the user. You should
use check box when presenting users with a group of selectable options that
are not mutually exclusive.
• ToggleButton: An on/off button with a light indicator.
• RadioButton: The RadioButton has two states: either checked or
unchecked.
• RadioGroup : A RadioGroup is used to group together one or more
RadioButtons.
• ProgressBarThe ProgressBar view provides visual feedback about some
ongoing tasks, such as when you are performing a task in the background.
• Spinner: A drop-down list that allows users to select one value from a set.
• TimePicker: The TimePicker view enables users to select a time of the day,
in either 24-hour mode or AM/PM mode.
• DatePicker: The DatePicker view enables users to select a date of the day.
Create UI Controls
• To create a UI Control/View/Widget you will have to define a view/widget in the
layout file and assign it a unique ID.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
</LinearLayout>
Then finally create an instance of the Control object and capture it from the layout,
use the following −
TextView myText = (TextView) findViewById(R.id.text_id);