Unit-2 - Chapter - 1 - MAD
Unit-2 - Chapter - 1 - MAD
Following are common attributes and will be applied to all the layouts:
ConstriantLayout:
ConstraintLayout is a ViewGroup subclass; whenever you open the
android studio framework the application will be present in the constraint
layout. In this we have to set the constraint in all four sides.
This is the default layout. It’s typically used to manage the positioning of
individual views rather than entire groups of views.
Ex: Designing a login screen for a mobile app using constraint layout in
Android.
LinearLayout:
The LinearLayout arranges views in a single column or a single row. Child
views can be arranged either horizontally or vertically in a single direction,
which explains the need for two different layouts—one for horizontal
rows of views and one for vertical columns of views. You can specify the
layout direction with the android:orientation attribute.
TableLayout :
TableLayout is a view that groups views into rows and columns. You use the
<TableRow> element to designate a row in the table. Each row can contain
one or more views.
In android, TableLayout will position its children elements into rows and
columns and it won’t display any border lines for rows, columns or cell.
RelativeLayout:
RelativeLayout is a view group that displays child views in relative
positions. The position of each view can be specified as relative to
sibling elements or relative to the parent.
Each view embedded within the RelativeLayout has attributes that
enable it to align with another view.
These attributes are as follows:
o layout_alignParentTop: Aligns the top edge of the view with
the top edge of the parent.
o layout_alignParentStart: Aligns the start edge of the view
with the start edge of the parent.
o layout_alignStart: Aligns the start edge of the view with the
start edge of another specified view.
o layout_alignEnd: Aligns the end edge of the view with the end
edge of another specified view.
o layout_below: Positions the view directly below another
specified view.
o layout_centerHorizontal: Centers the view horizontally
within the parent.
FrameLayout:
</ RelativeLayout>
ScrollView
A ScrollView is a special type of FrameLayout in that it enables users to
scroll through a list of views that occupy more space than the physical
display.
The ScrollView can contain only one child view or ViewGroup, which
normally is a LinearLayout. Android supports vertical scroll view as
default scroll view. Vertical scrollview scrolls element s vertically.
Android uses Horizontal ScrollView for scrolls element horizontally.
GridView
GridView is a ViewGroup that displays items in a two-dimensional,
scrollable grid.
ListView
ListView is a view group that displays a list of scrollable items.
wrap_content: Tells the view to size itself to the dimensions required
by its content.
Definition: Tells the view to become as big as its parent view, filling the
entire available space.
Example: If you have a Button with
android:layout_width="match_parent", the Button will stretch to fill the
width of its parent layout.
These attributes are important for controlling the layout and appearance of views
within your Android app's user interface.
attribute Description
layout_width Specifies the width of the view or ViewGroup
layout_height Specifies the height of the view or ViewGroup
Specifies extra space on the top side of the view
layout_marginTop
or ViewGroup
Specifies extra space on the bottom side of the
layout_marginBottom
view or ViewGroup
Specifies extra space on the left side of the view
layout_marginLeft
or ViewGroup
Specifies extra space on the right side of the
layout_marginRight
view or ViewGroup
layout_gravity Specifies how child views are positioned
Specifies how much of the extra space in the
layout_weight
layout should be allocated to the view
Specifies the x-coordinate of the view or
layout_x
ViewGroup
Specifies the y-coordinate of the view or
layout_y
ViewGroup
Adapting to Display Orientation :
As with almost all smartphones, Android supports two screen
orientations: portrait and landscape.
When the screen orientation of an Android device is changed, the
current activity being displayed is destroyed and re-created
automatically to redraw its content in the new orientation.
In other words, the onCreate() method of the activity is fired
whenever there is a change in screen orientation.
Portrait mode is longer in height and smaller in width, whereas
landscape mode is wider but smaller in height.
Being wider, landscape mode has more empty space on the right side
of the screen.
At the same time, some of the controls don’t appear because of the
smaller height.
Thus, controls need to be laid out differently in the two screen
orientations because of the difference in the height and width of the
two orientations.
There are two ways to handle changes in screen orientation:
Anchoring Views/Controls—
1. Anchoring involves setting the position of views (or controls) relative to the four
edges of the screen. When the screen orientation changes, the controls/views do
not disappear but are rearranged (the views adjust their positions accordingly,
maintaining a consistent layout and avoiding disappearing off-screen) relative to the
four edges.
2. For anchoring controls relative to the four edges of the screen, we use a
RelativeLayout container. The controls are aligned relative to the edges of the
container.
Defining layout for each mode—
This approach involves creating separate XML layout files for different
screen orientations (portrait and landscape). Each layout file is tailored to
suit the specific orientation, ensuring the UI is optimized for both modes.
•This vertical arrangement makes a few of the Button controls disappear when
the screen is in landscape mode. To use the blank space on the right side of the
screen in landscape mode, we need to define another layout file.
Resizing and Repositioning Views:
This approach involves dynamically adjusting the size and position of views within
a single layout file to respond to changes in screen orientation. Instead of
switching layouts, you modify view properties programmatically.
Anchoring Views Anchoring can be easily achieved by using
RelativeLayout. Consider the following main.xml file, which contains five
Button views embedded within the element:
CODE:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</RelativeLayout>
Android ActionBar is a menu bar that runs across the top of the activity screen
in android. Android ActionBar can contain menu items which become visible
when the user clicks the “menu” button.
Action bar is a combination of on-screen action items and overflow options.
Toolbar/Action Bar: This is a bar at the top of the screen in Android apps,
which contains the application logo, title, and navigation/menu items. It often
hosts contextual actions or options relevant to the current screen.
In general an ActionBar consists of the following four components:
o App Icon: App branding logo or icon will be displayed here.
o View Control: A dedicated space to display Application title. Also
provides option to switch between views by adding spinner or tabbed
navigation.
o Action Buttons: Some important actions of the app can be added here
o Action Overflow: All unimportant action will be shown as a menu.
Spinner
Spinner: A spinner typically appears as a small rectangular box with an arrow icon at
the right side. When the user taps on the spinner, a dropdown list of items appears
below it, allowing the user to select one.
Dropdown Menu: A dropdown menu, on the other hand, is a pop-up menu that
appears below the anchor view (such as a button or text field) when the user interacts
with it. It doesn't have a visible box or arrow icon associated with it.
How to set and Changing the Action Bar title:
Setting Toolbar as An ActionBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // get the reference
of Toolbar
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar
This example demonstrates How to set title for action bar in
android.
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("Home");
getSupportActionBar().setSubtitle("sairam");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher); // Replace with
your icon
}
textView.setText("Title is Home");
}
Changing the Action Bar title:
if (actionBar != null) {
actionBar.setTitle(newTitle);
}
At the activity level, the Activity class exposes methods that you can override.
Some common methods that you can override in your activities include the
following:
o On Key Up(): This is called when a key was released. This is not handled by
any of the views inside the activity.
o On Key Down(): This is called when a key was pressed. This is not handled
by any of the views inside the activity.
o On Menu Item Selected(): This is called when any item of the menu panel
is pressed by user.
o On Menu Opened(): This method is called when user opens the panel’s
menu.
View Level:
When any user interacts with a view, the corresponding view fires event.
When a user touches a button or an image button or any such view we have to
service the related service so that appropriate action can be performed. For
this, events need to be registered.