UNIT II Android Programming
UNIT II Android Programming
Android Programming
Android User Interface :
Understanding the components of a screen : Views and ViewGroups, LinearLayout ,
AbsoluteLayout, TableLayout, RelativeLayout , FrameLayout , ScrollLayout ,ScrollView
Utilizing Action Bar : Adding Action Items to the Action Bar, Customizing the Action Items and
Application Icon
Views and ViewGroups
● Android Layout is used to describe the user interface for an app or activity,
● it stores the UI elements that will be visible to the user.
● An android app’s user interface is made up of a series of View and ViewGroup
elements.
● The android apps will contain one or more activities and each activity is one screen
of the app.
● In Android apps, the two very central classes are the Android View class and
ViewGroup class.
● View or ViewGroup subclasses are used to build the GUI elements.
Views and ViewGroups
● As the name states View is singular and the group of Views is the ViewGroup.
Views and ViewGroups
View
● The View class is the base class or we can say that it is the superclass for all the
GUI components in android.
● EditText class is used to accept the input from users in android apps, which is a
subclass of View,
● TextView class which is used to display text labels in Android apps is also a
subclass of View.
● View refer to the android.view.View class, which is the base class of all UI classes.
android.view.View class is the root of the UI class hierarchy.
● So from an object point of view, all UI objects are View objects.
Views and ViewGroups
● Following are some of the common View subclasses that will be used in android
applications.
1. TextView
2. EditText
3. ImageView
4. RadioButton
5. Button
6. ImageButton
7. CheckBox
8. DatePicker
9. Spinner
10. ProgressBar and etc.
ViewGroup
Following are the commonly used ViewGroup subclasses used in android applications.
1. FrameLayout
2. WebView
3. ListView
4. GridView
5. LinearLayout
6. RelativeLayout
7. TableLayout and many more.
Views and ViewGroups
● Attributes
● Every View and ViewGroup object supports certain XML attributes
● Three categories
Example: id attribute
3. Layout parameters
● ID Attribute
• View objects have an integer ID associated with it, to uniquely
identify the View within the layout tree structure
• Example:
● android:id="@+id/btnOK"
@ indicates XML parser should parse this string
+ indicates that it’s a new resource name that must be created
Views and ViewGroups
Layout Parameters
● Those attributes for the View that are appropriate for the ViewGroup in which it
resides
○ Example: layout_width, layout_height
● Width and height can be specified with exact measurements or by specifying
○ wrap_content: view to size itself to the dimensions required by its content
○ match_parent: view to become as big as its parent view group will allow
Views and ViewGroups
Layout Positions
● Other methods
● LinearLayout is the most basic layout in android studio, that aligns all the children
sequentially either in a horizontal manner or a vertical manner by specifying the
android:orientation attribute.
● If one applies android:orientation=”vertical” then elements will be arranged one
after another in a vertical manner
● If you apply android:orientation=”horizontal” then elements will be arranged one
after another in a horizontal manner.
Linear Layout
Linear Layout
Linear Layout
Attributes Description
android:orientation How the elements should be arranged in the layout. It can be horizontal or vertical.
It specifies how an object should position its content on its X and Y axes.
android:gravity
Possible values are – center_vertical, fill, center, bottom, end, etc.
● An Absolute Layout allows you to specify the exact location .i.e., X and Y
coordinates of its children with respect to the origin at the top left corner of the
layout.
● The absolute layout is less flexible and harder to maintain for varying sizes of
screens that’s why it is not recommended.
● Although Absolute Layout is deprecated now.
Absolute Layout
Absolute Layout
View is a simple rectangle box that responds to the ViewGroup is the invisible container. It holds View and
user’s actions. ViewGroup
View is the SuperClass of All component like TextView, ViewGroup is a collection of Views(TextView, EditText,
EditText, ListView, etc ListView, etc..), somewhat like a container.
A View object is a component of the user interface (UI) A ViewGroup object is a layout, that is, a container of other
like a button or a text box, and it’s also called a widget. ViewGroup objects (layouts) and View objects (widgets)
View refers to the android.view.View class ViewGroup refers to the android.view.ViewGroup class
● The relative layout is used to arrange the child views in a proper order which
means arranging the child objects relative to each other.
● To arrange them in a proper order we need to use the relative layout.
● To arrange them we need some advanced properties.
● There are so many properties that are supported by relative layout. some of the
most used properties are listed below
RelativeLayout
layout_alignParentLeft It is set “true” to match the left edge of view to the left edge of parent.
layout_alignParentRight It is set “true” to match the right edge of view to the right edge of parent.
layout_alignParentTop It is set “true” to match the top edge of view to the top edge of parent.
layout_alignParentBottom It is set “true” to match the bottom edge of view to the bottom edge of parent.
layout_alignLeft It accepts another sibling view id and align the view to the left of the specified view id
It accepts another sibling view id and align the view to the right of the specified view
layout_alignRight
id.
layout_alignStart It accepts another sibling view id and align the view to start of the specified view id.
layout_alignEnd It accepts another sibling view id and align the view to end of specified view id.
layout_centerInParent When it is set “true”, the view will be aligned to the center of parent.
layout_centerHorizontal When it is set “true”, the view will be horizontally centre aligned within its parent.
layout_centerVertical When it is set “true”, the view will be vertically centre aligned within its parent.
layout_toLeftOf It accepts another sibling view id and places the view left of the specified view id.
layout_toRightOf It accepts another sibling view id and places the view right of the specified view id.
layout_toStartOf It accepts another sibling view id and places the view to start of the specified view id.
layout_toEndOf It accepts another sibling view id and places the view to end of the specified view id.
layout_above It accepts another sibling view id and places the view above the specified view id.
layout_below It accepts another sibling view id and places the view below the specified view id.
RelativeLayout
RelativeLayout
RelativeLayout
RelativeLayout
RelativeLayout
RelativeLayout
RelativeLayout
RelativeLayout
ScrollView
Attribute Description
Defines whether the scrollview should stretch its content to fill the
android:fillViewport
viewport.
Handling Display Orientation Changes
● Android devices come in various shapes and sizes, and users frequently switch
between portrait and landscape orientations.
● To ensure our applications look great in any scenario, we need to handle display
orientation changes.
● .In Android, we accomplish this by overriding the onConfigurationChanged
method.
● This method allows us to dynamically adapt our UI based on the current device
orientation.
@Override
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
Anchoring Views with ConstraintLayout
params.width = newWidthInPixels;
myButton.setLayoutParams(params);
Repositioning Views Dynamically
params.leftMargin = newLeftMarginInPixels;
params.topMargin = newTopMarginInPixels;
myButton.setLayoutParams(params);
Challenges in Screen Orientation Changes
● A user is deeply engaged in your app, and suddenly they rotate their device.
● The challenge here is maintaining the continuity of their experience without any
data loss or disruptions.
● Our goal is to overcome these challenges and create a smooth transition during
screen orientation changes.
Persisting State Information during Changes
super.onSaveInstanceState(outState);
@Override
super.onRestoreInstanceState(savedInstanceState);
}
Controlling the Orientation of the Activity
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
The Significance of Action Items
● The Action Bar is that strip at the top of our app that typically holds our app's title
and a few icons for various actions.
●
Components included in the ActionBar are:
<application
android:label=”@string/app_name”
</application>
Creating a new directory and design items of ActionBar
● To code the elements of ActionBar, create a new directory in the resource folder of
the application project files.
● Right-click on the res folder and selects New -> Directory. Give the name “menu” to
the new directory.
● create a new Menu Resource File by right click on the menu directory.
● a new file named “main.xml” must be created under the menu directory.
● In this file, one can declare the items which will be displayed as the action buttons
of the ActionBar.
● Every menu items, the following attributes are needed to be configured:
● android:title: Its value contains the title of the menu item that will be displayed when a
user clicks and holds that item in the app.
● android:id: A unique ID for the menu item that will be used to access it anywhere in the
whole application files.
● android:orderInCategory: The value of this attribute specify the item’s position in the
ActionBar.
○ provide the same value of this attribute for all items and the position will be defined
in the same order as they are declared in the code.
● app:showAsAction: This attribute defines how the item is going to be present in the
action bar. There are four possible flags to choose from:
○ a. always:
○ b. ifRoom:
○ c. never:
○ d. withText:
● android:icon: The icon of an item is referenced in the drawable directories through this
attribute.
Icon of an ActionBar Item
● In order to provide an icon to an item, right-click on the res folder, select new, and
then Image Asset. A dialog box will appear, choose the Icon Type as Action Bar and
Tab Icons.
● Choose assets type as “Clip Art” and select an image from the clip art collection.
Provide a desired name to the icon.
● Click on Next, then Finish.
● This icon will now get loaded in the drawable directory of the res folder.
● The name provided by the developers to these icons will now be used to reference
the item’s icon resource.
Color of the ActionBar
@Override
switch (item.getItemId()) {
case R.id.action_item:
return true;
default:
return super.onOptionsItemSelected(item);
}
Adding Action Items to the Action Bar
@Override
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Application Icon
● Our application icon is a crucial part of our app's identity and branding.
● It's often the first thing users notice about our app. So, let's see how we can
customize the application icon.
● Customizing the application icon is quite straightforward.
● In the AndroidManifest.xml file, within the <application> element, we can use the
android:icon attribute to set our app's launcher icon and the android:logo attribute
to set a different icon for the Action Bar: