Unit 2
Unit 2
Using activity, we can insert elements and place UI elements in a single screen.
Android Activity is the subclass of ContextThemeWrapper class. Below is the complete class hierarchy for
Activity.
Object
Context
ContextWrapper
ContextThemeWrapper
Activity
Activity Launched
onCreate()
onStart() onRestart()
onDestroy()
Activity
Destroyed
The following callbacks, or events, are defined by the Activity class. It's not required that you use every
callback method. But it's crucial that you comprehend each one and put those into practice to make
sure your app operates as consumers would anticipate.
File: MainActivity.java
package example.javatpoint.com.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}
Android Fragments:
Fragments are the part of activity or we can say it’s a sub-activity. This helps to represent multiple
screens in any activity.
Fragment lifecycle gets affected by activity lifecycle as fragments are embedded in activity only.
Just like Activity Manager, Fragment Manager is responsible for making interactions between fragment
objects.
Types of Fragments:
Fragment LifeCycle:
The following callbacks, or events, are defined by the Fragment class. It's not required that you use
every callback method. But it's crucial that you comprehend each one and put those into practice to
make sure your app operates as consumers would anticipate.
ViewGroup
Types of Layouts
1. Linear Layout: Linear Layout is a view group which aligns all the elements in a single direction,
horizontally or vertically. You can specify direction using orientation attribute
(android:orientation).
<LinearLayout>
</LinearLayout>
Horizontal Linear View
</LinearLayout>
Relative Layout
3. Table Layout: Table Layout displays elements in rows and columns. It displays all children in
rows and columns with no borders between rows and columns or for each cell. For defining
rows we use “<TableRow> </TableRow>” tag.
<TableLayout>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
</TableLayout>
Table Layout
4. Absolute Layout: Absolute Layout allows developer to specify exact location, i.e., X and Y
coordinates for its elements with the respect to the top-left corner at the top of the layout. This
layout is flexible and harder to maintain in different sizes of screen, that’s why it is highly not
recommended.
Absolute Layout
5. Frame Layout: A ViewGroup subclass called Android Framelayout is used to define the
arrangement of several views stacked on top of one another to create the appearance of a
single view screen. In general, FrameLayout is only a way to display a single view by blocking a
specific section of the screen.
<FrameLayout>
</FrameLayout>
Frame Layout
6. List View: List view displays elements in the form of list with scrollable items. As subclasses of
AdapterView, ListView and GridView can be filled with data by attaching them to an Adapter,
which builds a View for each data entry and pulls data from an external source.
Example:
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
ITEM 1
ITEM 1
ITEM 1
ITEM 1
ITEM 1
ITEM 1
ITEM 1
7. Grid View: Grid view displays elements in form of grid with scrollable items. One kind of
AdapterView that shows things in a scrolling, two-dimensional grid is called a GridView. This grid
layout inserts items from either an array or a database. This data is displayed using the adapter,
and GridView is joined with the adapter using the setAdapter() method. The major job of the
GridView adapter is to retrieve data from an array or database and add it to the relevant item
that will be shown in GridView. The GridView structure looks like this. Both Java and Kotlin
programming languages will be used to develop this project for Android.
<GridView>
</GridView>
Grid View
Layout Attributes:
A View is a place which draws something on screen using which user can interact with and ViewGroup is
a container that holds views to define the layout of user interface.
When creating your interface, make an effort to select interface pieces that are predictable and
consistent. Users have grown accustomed to particular features functioning in a certain way, whether
they realize it or not. Choosing to employ such elements when it makes sense will contribute to task
completion, efficiency, and satisfaction.
1. Input Controls: checkboxes, radio buttons, dropdown lists, list boxes, buttons, toggles,
text fields, date field
Input Controls:
S.No Input Control & It’s Description Examples
1 TextView
This control is used to display text to the user.
2 EditText
EditText is a predefined subclass of TextView that
includes rich editing capabilities.
3 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.
4 Button
A push-button that can be pressed, or clicked, by
the user to perform an action.
5 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.
6 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.
7 ToggleButton
An on/off button with a light indicator.
8 RadioButton
The RadioButton has two states: either checked or
unchecked.
9 RadioGroup
A RadioGroup is used to group together one or
more RadioButtons.
10 Spinner/DropDown
A drop-down list that allows users to select one
value from a set.
11 TimePicker
The TimePicker view enables users to select a time
of the day, in either 24-hour mode or AM/PM
mode.
12 DatePicker
The DatePicker view enables users to select a date
of the day.
2. Navigational Components: breadcrumb, slider, search field, pagination, slider, tags, icons.
S. No. Input Control & It’s Description Examples
1 Search Field
A search box allows users to enter a
keyword or phrase (query) and submit
it to search the index with the
intention of getting back the most
relevant results. Typically search fields
are single-line text boxes and are
often accompanied by a search
button.
2 Breadcrumb
Breadcrumbs allow users to identify
their current location within the
system by providing a clickable trail of
proceeding pages to navigate by.
3 Pagination
Pagination divides content up
between pages, and allows users to
skip between pages or go in order
through the content.
4 Tags
Tags allow users to find content in the
same category. Some tagging systems
also allow users to apply their own
tags to content by entering them into
the system.
5 Sliders
A slider, also known as a track bar,
allows users to set or adjust a value.
When the user changes the value, it
does not change the format of the
interface or other info on the screen.
6 Icons
An icon is a simplified image serving
as an intuitive symbol that is used to
help users to navigate the system.
Typically, icons are hyperlinked.
7 Image Carousel
Image carousels allow users to browse
through a set of items and make a
selection of one if they so choose.
Typically, the images are hyperlinked.
3. Information Components: tooltips, icons, progress bar, notifications, message boxes, modal
windows
3 Tool Tips
A tooltip allows a user to see hints
when they hover over an item
indicating the name or purpose of the
item.
4 Message Boxes
A message box is a small window that
provides information to users and
requires them to take an action before
they can move forward.