Join Albert Lo for an in-depth discussion in this video Account selection using ActionBar UI, part of Android Development: Modern Android UI Design Elements.
- [Instructor] Now that we are finished with our changes with Google Photos Fragment, let's head over to Main Activity. So I'm going to open the project view. And we will select the Android project view and select main activity and we close our project view. So what we want to do in our main activity is add a ui control in our action bar that will ask the user to select a Google account.
So lets scroll up to on Create Options menu. That's around line 61. So let's go up there. In this method, we see that r.menu.main is being inflated. So let's go into that with command B. And we're going to add a new menu item to our action bar. So, we'll add a tag item. And we'll give it an id. And our id will be select account.
The order and category, we want it to appear before the settings. So we'll give it a value of 50. Title, we're going to add a title account and I think we can reuse account name. Let's just check. Account name has a format specifier. So let's create our own account name. So we'll call it account and we'll convert it to a string resource and we'll call this account menu item.
We're also going to add an icon and icon name is something I've already included in our project. It will be ic account circle black and we'll also specify showAsAction always. Now we're done with our changes. Let's add the enclosing tag. And now we're ready to switch back to our main activity. So in our onCreateOptionsMenu, we have our menu item for selecting account.
Let's get a reference to that. So let's create a new class member. We're going to call it account menu and it's type MenuItem. Let's scroll back to onCreateOptionsMenu and we're going to call menu.findItem and we'll pass is the id that we created select account.
We'll initialize the visibility of the menu item to invisible. Now let's go to onOptionsItemSelected. And instead of the logic for action settings, let's replace that with select account and here what we're going to do is call our Google Photos Library method PicasaClient pickAccount.
Now there's a couple of other items that we need to update here. The first one is onActivityResult in the else case. We want to call this super method of the parent class onActivityResult's parent class, passing in the same parameters, requestCode, resultCode, and intent. Let's scroll down to the onClick method. And our onClick method, if we select our Google Photos item from the side nav drawer, we want to show the account menu from the action bar.
So let's call account menu and we'll set the icon to visible There is a couple of items that we need to update here. In setFragment, the transaction at the very end, we need to add a commit. And also, we will replace the string with Google Photos Fragment's method call for SimpleName.
And one other thing we need to do is, we go back up to our drawer where we inflate our drawer. Our drawer layout is initialized in inflateViews. There should be only one instance of drawer layout. So let's delete this reference here. We already have a class member for DrawerLayout. So let's go down to onNavigationItemSelected.
Again, we should only have one instance. This had already been done in inflate views, so we can delete that. And let's scroll up to onBackPressed. We already have our instance of DrawerLayout so we don't need this find by view id method. Let's delete that. And just for sanity check, we'll add in a null check here. And one last thing we want to do is go into our layout XML.
Let's go into app bar main and into our content main. Let's replace the ConstraintLayout with RelativeLayout. And we will replace our placeholder text constraints with centerInParent. Now we are finished with our changes to content main layout. Let's switch back to our MainActivity. One last thing we need to add is to register our Main Activity with our Google Photos Library and there's a method call.
It's called PicasaClientAttachActivity. So let's call that here. And we'll pass in a reference to our MainActivity here. Now we are finished with our changes with MainActivity and we're ready to test.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox