4 - 4 - user interface classes - part 4 (14-21)

6
[BLANK_AUDIO] Hi. I'm Adam Porter. And this is Programming Mobile Applications for Android Handheld Systems. >> In addition to what I've just shown you 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 activity when the user clicks on a particular menu item. 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 a main activity, which hosts a single fragment, the title fragment.

Upload: saumith-dahagam

Post on 21-Jul-2016

8 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 4 - 4 - User Interface Classes - Part 4 (14-21)

[BLANK_AUDIO]Hi.I'm Adam Porter.And this is Programming MobileApplications for Android Handheld Systems.>> In addition to what I've just shownyouMenus can also support a number of moreadvanced features.For example, you can put related menuitems into agroup so you can process and manipulatethem as a unit.You can also find the short cut keys tospecific menu items so you can access themmore quickly.And you can bind In tens to menu items.So for instance you can start a particularactivitywhen the user clicks on a particular menuitem.So I've mentioned the ActionBar a fewtimes now.The ActionBar was added in Android 3.10And kind of mimics theapplication bar that you often see indesktop app, desktop applications.You know, that bar at the top of theapplication window that just says thingslike File, Edit, Help and those things.And the basic idea behindthe action bar.Is that rather than hiding actions behinda traditional pop-up menu,you want to give the users quick access tothe actions that they're likely to use.Let's look at some uses of the action barNow thefirst example I'll give goes back to ourlesson on fragments.You remember those quote viewerapplications, 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, theseitems areprovided by three different classes Someitems come from themain activity, some come from the fragmentthat displays thetitles, and some come from the fragmentthat displays the quotes.Let's takea look.Here's my emulated tablet.Now I'll start the application.And as before, when it starts up, there'samain activity, which hosts a singlefragment, the title fragment.

Page 2: 4 - 4 - User Interface Classes - Part 4 (14-21)

Now if you look at the action bar at thetop of the tablet,you see that there are two bits of text inthe upper right corner.One saysActivity Action.And the other says title action.That first piece of text was put there bythe quote viewer activity.The second was put there by the titlefragment.And if I click on these bits of text,you'll see some text pop up.Letting you know which object is actuallycarrying out the actions associatedwith that action bar item.Now if I click on one of the titles, youremember thatthis causes the quote fragment to bedynamically 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, andthesecond action, that gets put in theoverflow area.SoI'll click on the main action, and again,you can seethe text saying, that this action isprovided by the quote fragment.If I click on the overflow icon, thiscausesthe second item provided by the quotefragment, to appear.And if I now click on that pop up, youagain see an associated text pop up.Now let's look at how this is implementedin the source code.Here I've opened the application in theIDE.I'll first open the code viewer activityfile.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 inthe activity underscore menu dot XML file.And this looks like what we've alreadyseen.But one difference is the show as actionattribute.Its value is if room Or, with the text.And this means that Android should showthisitem in the action bar, if there's room.But it should put it in the overflow areaif otherwise.It also means the item should be shown as

Page 3: 4 - 4 - User Interface Classes - Part 4 (14-21)

text, rather than by displaying san iconNow,I'll open up the title fragmentt fli,file.And again, there're calls tothe onCreateOptionsMenu andonOptionsMenuItemSelected methods.The one difference here,is that because title fragment is afragment, wealso have to issue the command Set hasoptions true.Which we do in the on create method.And last I'll open up the quote fragmentfile.Again, not much new here.My quote fragment has it's menu file andquote underscore menu .XML And lets lookat that.[BLANK_AUDIO]This menu has two items.One has it'sshow as action attribute.Set to if room, or with text.Just like we saw before.The other one though, has its show as anattribute.Set to never and so it will always appearin the overflow area and never in theaction bar.[BLANK_AUDIO]Another use of the ActionBar is to helpprovide aconsistent way of switching, betweendifferent views, in an application.When using the ActionBar this way, thescreen is divided into two sections.A tab area And a content area.TheActionBar.Tabclass allows you to associate a fragment,with each tab indicator,in the tab area.Now only one tab indicator, can beselected at any one time, so when the userselects a particular tab indicator Itsfragment can be shown in the content area.If the user then selects a different tab,adifferent fragment can be shown in thecontent area.So here's a sample application calledUITabLayout that usesthe ActionBar.Tab class to switch betweentwo instances.Of a fragment that uses the grid viewlayout that we discussed earlier.Here I'll start the UI tab layoutapplication.The application displays two tabindicators,

Page 4: 4 - 4 - User Interface Classes - Part 4 (14-21)

one labeled flowers, and one labeledanimals.The flowers tab is currently selected.And in fact does exactly what we saw inthe UI GridView application.Now I'll select the animals tab.As you can see, the application is nowdisplaying another GridViewfragment but this time it's showing imagesof dogs rather than flowers.Let's take a look at the source code forthis application.Here's the application open in the IDE.I'll now open the tab layout activity fileand go to the on create method.First the code get's the action bar, thenitconfigures the action bar to operate as atab.Next, I create the grid view fragment,givingit the list of images that it shoulddisplay.And in this case, that's the flowerimages.Finally, I create and configure a new tabindicator.And attachit to the action bar.And I'll do essentially the same thingwith the other tab.Now when I added the tab notice that Ialso created an instance of somethingcalled the Tab Listener.And this is an object that will becalled when the user selects and unselectsindividual tabs.Let me scroll down to that code.Now here'sthe on tab selected method.I'm going to tab selected this code addsit's fragment to the hosting activity.Here's the on tab unselected method.When a tab is unselected this coderemoves the current fragment From thehosting activity.The last thing I want to discuss aredialogs.A dialog is a kind of independentsubwindow, thatis used by an activity for shortcommunications with users.Some dialog classes provided by Andriodinclude the alert dialogThe progress dialog, and the date and timepicker dialogs.Let's look at a sample applicationthat uses both the AlertDialog, and theProgressDialog.Here's the UIAlertDialog application.

Page 5: 4 - 4 - User Interface Classes - Part 4 (14-21)

When I start it up, it displays onebutton.That the user can press to initiateapplication shutdown.Let me hit that button now.When I do that, you see that theapplication popsup a dialog, an alert dialog, that shows amessage.Do you really want to exit?And it allowsthe user to respond in this case, eitheryes or no.I'll hit no now.It simply dismisses the alert dialog andreturns me back to the application.Let's say time goes by and now I really dowant exit.So I'll hit the shutdown button again.Which again brings up the alert dialog.This time, however, I'll hit the yesbutton on the alert dialog.And at this point, the alert dialog isdismissed, anda new dialog, this time a progress dialog,is displayed.Which shows a graphic spinner.To let me know that the shut down processis proceeding.And eventually the shutdown finishes andthe application terminates itself.Lets see what this looks like in thesource code.So here's the application of the IDE I'llopen up thealert dialog activity file, and go to itson create method.As you can see, when the user presses theshutdown button, the show dialog fragmentmethod is invoked passingin an ID for the desired dialog, the showdialogfragment method Creates and instance ofthe alert dialog fragment.And then calls the show method on it.Lets look at that class.Alert dialog fragment is a sub class ofdialogfragment and it has an on create dialogmethod.This method will be called in response tothe show method being invoked.And this method creates a newalertDialog.builder instance.The methods of a builder almost alwaysreturn the current object.And this is useful because it allows youto create an object And then just keeptacking on method calls one after another

Page 6: 4 - 4 - User Interface Classes - Part 4 (14-21)

to configure that object.In fact, here you can see a call that setmessage and right after thata call that set cancelable, and then acall that set negative button and soforth.And once you've set all the things thatyou want.You end with a call to the create method,which effectively puts together all ofthe previous calls and returns the finalconfigured object.Now once this dialog is displayed, if theuser selectsNo Then there's a call to shut downcontinue with the parameter false.If the use instead select yes then there'sacall to shut down continue with theparameter true.Lets go to the shutdown continue method.So if should continue is false Then wedismiss thealert dialog and everything goes on as ifnothing had happened.It should continue is true however, wedismiss the alert dialog, andthen call show dialog passing in theprogress tag ID.And this call creates a progress dialogfragment object.And then calls the show method on it.Eventually, we end up at theonCreateDialog method, in theProgressDialogFragment class.And here we make a new progress dialog,set its message to Activity Shutting DownAnd then calls set indeterminate true,which allowsthe dialog to stay visible until itsdismissed.Sothat's all for our discussion of android'suser interface classes.Please join me next time, when we'lldiscuss, user notifications.See you then.[BLANK_AUDIO]