Android navigation drawer – Account picker

This post describes how to create a navigation drawer with account picker. Android navigation draweris a new UI pattern introduced in the last I/O. The android navigation drawer is esentially a layer that appears from left to right in the left side of the screen. I want to explain here how to add an account picker to the android navigation drawer.To do it I will use the new Google Service API with Drive SDK mixing them in order to achieve this goal. If you look at Google Drive App you will notice that you can choose your account directly from the left drawer instead of picking it from the settings. In this post, I will show how we can get the folder in our Google drive account selecting it from the left navigation drawer.

Set up the android navigation drawer layout

I won’t give into the details how to use navigation drawer because there are already too many docs talking about it, so this post assumes you are already familiar with this pattern. If you want to know more you can give a look at “Navigation Drawer”. Let’s setup our layout first, we need:

A spinner to select our account

The navigation drawer items

As we know already the drawer must be the first element in our layout so we have

At line 7 we introduce a FrameLayout in order to handle the UI content dynamically. Instead of using the ListView directly after the FrameLayout (as in the official doc), we introduce a linear layout (line 12) to hold the spinner (line 25) and the menu items. The spinner will be populated with the active user accounts so that when user activate the navigation drawer he will find the account picker too.

Account picker: Populate the spinner with accounts

The next step handling android navigation drawer is creating the account picker so that the user can choose the account to use. To do it we populate the spinner with the active account configured on the smartphone. We use the AccountManager to get the list of the accounts:

User account selection

Once we have our account list inside the navigation drawer, we have to handle the user account selection. To do that we have to override onItemSelected, so that we can set up the right process to require the authorization if needed and set up Drive API correctly. So we have:

At line 8 we select the account chosen. Then we save the credential info chosen (line 9) by the user and start the async process to retrieve the user folder (line 12,13).

Authorization and Google Drive Access

Now we want to access to the Google Drive data and retrieve all the folder in the root directory. We know that to do it we have to be authenticated and authorized and this process is made by two different steps:

Choose the account

Authorize the account

The first step is already done when we select our account using the spinner (see above), so we have to focus on the second step (the authorization). The first thing we have to do is trying to access to the drive data so that we can know if we are already authorized or we need an authorization. The process to access to the remote drive data uses an HTTP connection so we can’t handle it in our main thread, so that we have to create an async process using AsyncTask.

As we can see, we simply try to retrieve user folder (line 7-10) and we catch UserRecoverableAuthIOException (line 13) to get informed if we need to be authorized. If we have to be authorized then we start a new Activity (line 14) asking the authorization request to the user.

Now we have to handle the authorization request result so we implement onActivityResult in our main activity in this way:

At line 6 if we have a positive result (RESULT_OK) then we start again the async process to retrieve user folders.

Beautify the code

By now we haven’t still used the FrameLayout, so it is time to use it. As we said at the beginning this frame is used to display some content in the UI. We can change this content dynamically using Fragments. We want to show a “wait” info while we are accessing to the user drive account and then the folder retrieved. To do it we need simply two simple Fragment:

WaitFragment that shows the wait symbol while we access to the drive

ListFragment that shows the folder list

I won’t go into the details because they’re very simply I want to underline how to change the content in the FrameLayout. If we come back to our AsyncAuth we can notice it is implemented just the doBackgroud method. We have two other methods to exploit in this way:

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

Haider

Hi there,

Thanks for the tut, really useful. However, when I tried to run the code, it was all fine until selected an account from the picker. Then I got a fatal error at line 165: FileList files = request.execute();

I am not sure if you might know what the reason could be?

Thanks,

survivingwithandroid

It could be your account isn't authorized. There are some steps behind the code that must be completed in order to enable the account. Can you post the log?

Sign up for our Newsletter

Enter your email and stay on top of things,

Get SwA updates delivered to your inbox. Article, tips, source code

×

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.