Android M-4
Android M-4
Android M-4
MODULE 4
Module 4
SYLLABUS
Android menus - creating menus, working with menu groups, responding to menu items, icon
menu, sub menu, context menu, dynamic menus, loading menu through XML, popup menus.
Fragments in Android structure of fragment, fragment life cycle, fragment transaction and back
stack, fragment manager, saving fragment state, persistence of fragments, communications with
fragments, startActivity() and set TargetFragment().
using dialogs in android, dialog fragments, working with toast, Implementing action bar - tabbed
navigation action bar activity, implementing base activity classes, tabbed action bar and tabbed
listener, debug text view layout, action bar and menu interaction, list navigation action bar
activity, spinner adapter, list listener, list action bar, standard navigation action bar activity,
action bar and search view, action bar and fragments.
Short Answer Questions
1. Name the method which is called when the menu item has been invoked?
Ans :onOptionsItemSelected()
2. A single action bar can control multiple activities, True or False.
Ans:True.
3. What are the attributes of Menu Item?
Ans:android:id,android:icon and android:title
4. How does URIMatcher work and what is it used for?
Ans: UriMatcher is a handy class when you are writing a ContentProvider or some other class
that needs to respond to a number of different URIs. In your example, a user could query your
provider with URIs such as:myprovider://personal -> to access the data
Paragraph Questions
5. Explain the regions in alert dialog window.
Ans : There are three regions in an alert dialog. These are:
Title
This is optional and it should be generally used when the content area is occupied by a
detailed message, a list, or a custom layout. If you need to state a simple message or
question, you don't need a title.
Content Area
This area is used to display any message, a list, or other custom layout.
Action Buttons
Alert Dialog allows maximum three action buttons in a dialog
6. What are adapters? How it is different from list controls?
Ans: An adapter basically connects the User Interfaces and the Data Source. According to
Android officials, “An Adapter object acts as a bridge between an AdapterView and the
data for that view. Android Adapters basically provides access to the data items.”
List controls are used to display collections of data. ... In other words, list controls contain
collections of child views. The purpose of an adapter is to manage the data for an
AdapterView and to provide the child views for it.
7. What are the attributes of menu item? Explain.
Ans : The <item> element supports several attributes you can use to define an item's
appearance and behavior. The items in the above menu include the following attributes:
android:id : A resource ID that's unique to the item, which allows the application to
recognize the item when the user selects it.
android:icon : A reference to a drawable to use as the item's icon.
android:title : A reference to a string to use as the item's title.
android:showAsAction : Specifies when and how this item should appear as an action item
in the app bar.
Essay Questions
8. Explain fragment life cycle?
Ans: Android Fragment is the part of activity, it is also known as sub-activity. There can be
more than one fragment in an activity. Fragments represent multiple screen inside one
activity.
Android fragment lifecycle is affected by activity lifecycle because fragments are included in
activity.
Each fragment has its own life cycle methods that is affected by activity life cycle because
fragments are embedded in activity. The FragmentManager class is responsible to make
interaction between fragment objects.
The lifecycle of android fragment is like the activity lifecycle. There are 12 lifecycle methods for
fragment.
Android Fragment Lifecycle Methods
Methods Description
The very first method to be called when the fragment has been associated
with the activity. This method executes only once during the lifetime of a
onAttach() fragment.
This method initializes the fragment by adding all the required attributes
onCreate() and components.
System calls this method to create the user interface of the fragment. The
root of the fragment’s layout is returned as the View component by this
onCreateView() method to draw the UI.
It indicates that the activity has been created in which the fragment
exists. View hierarchy of the fragment also instantiated before this
onActivityCreated() function call.
The system invokes this method to make the fragment visible on the
onStart() user’s device.
It indicates that the user is leaving the fragment. System call this method
onPause() to commit the changes made to the fragment.
System calls this method to clean up all kinds of resources as well as view
onDestroyView() hierarchy associated with the fragment.
Methods Description
The system executes this method to disassociate the fragment from its
onDetach() host activity.
9. Explain how to create menu and menu groups? How to load menu through xml?
Android Studio provides a standard XML format for type of menus to define menu items.
We can simply define the menu and all its items in XML menu resource instead of
building the menu in the code and also load menu resource as menu object in the activity
or fragment used in our android application.
Here, we should create a new folder menu inside of our project directory (res/menu) to
define the menu and also add a new XML file to build the menu with following elements.
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
<menu> It is the root element which helps in defining Menu in XML file and it also holds
multiple elements.
<item> It is used to create a single item in menu. It also contains nested <menu> element in
order to create a submenu.
<group> It is an optional and invisible for <item> elements to categorize the menu items so
they can share properties like active state, visibility.
activity_main.xml
If we want to add submenu in menu item, then we need to add a <menu> element as the
child of an <item>.
<item android:id="@+id/file"
android:title="@string/file" >
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu>
Android Different Types of Menus
In android, we have a three types of Menus available to define a set of options and actions in
our android applications.
The Menus in android applications are following –
Android Options Menu
Android Context Menu
Android Popup Menu
Android Options Menu – Android Options Menu is a primary collection of menu items in an
android application and useful for actions that have a global impact on the searching
application.
Android Context Menu – Android Context Menu is a floating menu only appears when user click
for a long time on an element and useful for elements that effect the selected content or context
frame.
Android Popup Menu – Android Popup Menu displays a list of items in a vertical list which
presents to the view that invoked the menu and useful to provide an overflow of actions that
related to specific content.