Sie sind auf Seite 1von 6


I'm Adam Porter.
And this is Programming Mobile
Applications for Android Handheld Systems.
>> In addition to what I've just shown
Menus can also support a number of more
advanced features.
For example, you can put related menu
items into a
group so you can process and manipulate
them as a unit.
You can also find the short cut keys to
specific menu items so you can access them
more quickly.
And you can bind In tens to menu items.
So for instance you can start a particular
when the user clicks on a particular menu
So I've mentioned the ActionBar a few
times now.
The ActionBar was added in Android 3.10
And kind of mimics the
application bar that you often see in
desktop app, desktop applications.
You know, that bar at the top of the
application window that just says things
like File, Edit, Help and those things.
And the basic idea behind
the action bar.
Is that rather than hiding actions behind
a traditional pop-up menu,
you want to give the users quick access to
the actions that they're likely to use.
Let's look at some uses of the action bar
Now the
first example I'll give goes back to our
lesson on fragments.
You remember those quote viewer
applications, well, in this application,
fragment dynamic layout with action bar,
I've added some items to the action bar.
Now, as you'll see in a minute, these
items are
provided by three different classes Some
items come from the
main activity, some come from the fragment
that displays the
titles, and some come from the fragment
that displays the quotes.
Let's take
a look.
Here's my emulated tablet.
Now I'll start the application.
And as before, when it starts up, there's
main activity, which hosts a single
fragment, the title fragment.
Now if you look at the action bar at the
top of the tablet,
you see that there are two bits of text in
the upper right corner.
One says
Activity Action.
And the other says title action.
That first piece of text was put there by
the quote viewer activity.
The second was put there by the title
And if I click on these bits of text,
you'll see some text pop up.
Letting you know which object is actually
carrying out the actions associated
with that action bar item.
Now if I click on one of the titles, you
remember that
this causes the quote fragment to be
dynamically added to the layout.
Well the quote fragment also adds.
Some items to the action bar, dynamically.
In this case there's a main action, and
second action, that gets put in the
overflow area.
I'll click on the main action, and again,
you can see
the text saying, that this action is
provided by the quote fragment.
If I click on the overflow icon, this
the second item provided by the quote
fragment, to appear.
And if I now click on that pop up, you
again see an associated text pop up.
Now let's look at how this is implemented
in the source code.
Here I've opened the application in the
I'll first open the code viewer activity
There, we see the on create options menu,
and on options menu item selected methods.
Same as we've talked about before.
Not much new here.
Let's take a look at the menu layout in
the activity underscore menu dot XML file.
And this looks like what we've already
But one difference is the show as action
Its value is if room Or, with the text.
And this means that Android should show
item in the action bar, if there's room.
But it should put it in the overflow area
if otherwise.
It also means the item should be shown as
text, rather than by displaying san icon
I'll open up the title fragmentt fli,
And again, there're calls to
the onCreateOptionsMenu and
onOptionsMenuItemSelected methods.
The one difference here,
is that because title fragment is a
fragment, we
also have to issue the command Set has
options true.
Which we do in the on create method.
And last I'll open up the quote fragment
Again, not much new here.
My quote fragment has it's menu file and
quote underscore menu .XML And lets look
at that.
This menu has two items.
One has it's
show as action attribute.
Set to if room, or with text.
Just like we saw before.
The other one though, has its show as an
Set to never and so it will always appear
in the overflow area and never in the
action bar.
Another use of the ActionBar is to help
provide a
consistent way of switching, between
different views, in an application.
When using the ActionBar this way, the
screen is divided into two sections.
A tab area And a content area.The
class allows you to associate a fragment,
with each tab indicator,
in the tab area.
Now only one tab indicator, can be
selected at any one time, so when the user
selects a particular tab indicator Its
fragment can be shown in the content area.
If the user then selects a different tab,
different fragment can be shown in the
content area.
So here's a sample application called
UITabLayout that uses
the ActionBar.Tab class to switch between
two instances.
Of a fragment that uses the grid view
layout that we discussed earlier.
Here I'll start the UI tab layout
The application displays two tab
one labeled flowers, and one labeled
The flowers tab is currently selected.
And in fact does exactly what we saw in
the UI GridView application.
Now I'll select the animals tab.
As you can see, the application is now
displaying another GridView
fragment but this time it's showing images
of dogs rather than flowers.
Let's take a look at the source code for
this application.
Here's the application open in the IDE.
I'll now open the tab layout activity file
and go to the on create method.
First the code get's the action bar, then
configures the action bar to operate as a
Next, I create the grid view fragment,
it the list of images that it should
And in this case, that's the flower
Finally, I create and configure a new tab
And attach
it to the action bar.
And I'll do essentially the same thing
with the other tab.
Now when I added the tab notice that I
also created an instance of something
called the Tab Listener.
And this is an object that will be
called when the user selects and unselects
individual tabs.
Let me scroll down to that code.
Now here's
the on tab selected method.
I'm going to tab selected this code adds
it's fragment to the hosting activity.
Here's the on tab unselected method.
When a tab is unselected this code
removes the current fragment From the
hosting activity.
The last thing I want to discuss are
A dialog is a kind of independent
subwindow, that
is used by an activity for short
communications with users.
Some dialog classes provided by Andriod
include the alert dialog
The progress dialog, and the date and time
picker dialogs.
Let's look at a sample application
that uses both the AlertDialog, and the
Here's the UIAlertDialog application.
When I start it up, it displays one
That the user can press to initiate
application shutdown.
Let me hit that button now.
When I do that, you see that the
application pops
up a dialog, an alert dialog, that shows a
Do you really want to exit?
And it allows
the user to respond in this case, either
yes or no.
I'll hit no now.
It simply dismisses the alert dialog and
returns me back to the application.
Let's say time goes by and now I really do
want exit.
So I'll hit the shutdown button again.
Which again brings up the alert dialog.
This time, however, I'll hit the yes
button on the alert dialog.
And at this point, the alert dialog is
dismissed, and
a new dialog, this time a progress dialog,
is displayed.
Which shows a graphic spinner.
To let me know that the shut down process
is proceeding.
And eventually the shutdown finishes and
the application terminates itself.
Lets see what this looks like in the
source code.
So here's the application of the IDE I'll
open up the
alert dialog activity file, and go to its
on create method.
As you can see, when the user presses the
down button, the show dialog fragment
method is invoked passing
in an ID for the desired dialog, the show
fragment method Creates and instance of
the alert dialog fragment.
And then calls the show method on it.
Lets look at that class.
Alert dialog fragment is a sub class of
fragment and it has an on create dialog
This method will be called in response to
the show method being invoked.
And this method creates a new
alertDialog.builder instance.
The methods of a builder almost always
return the current object.
And this is useful because it allows you
to create an object And then just keep
tacking on method calls one after another
to configure that object.
In fact, here you can see a call that set
message and right after that
a call that set cancelable, and then a
call that set negative button and so
And once you've set all the things that
you want.
You end with a call to the create method,
which effectively puts together all of
the previous calls and returns the final
configured object.
Now once this dialog is displayed, if the
user selects
No Then there's a call to shut down
continue with the parameter false.
If the use instead select yes then there's
call to shut down continue with the
parameter true.
Lets go to the shutdown continue method.
So if should continue is false Then we
dismiss the
alert dialog and everything goes on as if
nothing had happened.
It should continue is true however, we
dismiss the alert dialog, and
then call show dialog passing in the
progress tag ID.
And this call creates a progress dialog
fragment object.
And then calls the show method on it.
Eventually, we end up at the
onCreateDialog method, in the
ProgressDialogFragment class.
And here we make a new progress dialog,
set its message to Activity Shutting Down
And then calls set indeterminate true,
which allows
the dialog to stay visible until its
that's all for our discussion of android's
user interface classes.
Please join me next time, when we'll
discuss, user notifications.
See you then.