Thursday, September 4, 2014

Getting Started with Dropbox Core API for Android

Dropbox has several APIs listed on their developer portal, and that can be a little confusing at first. Take your time to poke through each one, and find out for yourself which one works best for you.

I wanted to learn how to integrate Dropbox functionality into my Android app, so I did a Google search for "dropbox android sdk". The first result led me to the download page for the Android SDK for the Core API, and at that point not knowing that there were multiple SDKs for the different APIs, I decided to start with that.

This article will focus specifically on getting the starter project for the Core API set up in Eclipse/ADT.

1. Download the Android SDK for Core API. Click on the download link on the download page.


2. Unzip the downloaded SDK, then import the sample project.


3. If you try running the project now, you see this:


So go to the site and register your app to obtain the app key and app secret. Just go to developers.dropbox.com and click on "App Console" on the left hand navigation pane.

Sign into your Dropbox account to continue, and you'll see this: 

Click on the Create app button on the top left corner of the page as shown in the screenshot above. You'll get a form and you'll have to also verify your email address when you create the app if you haven't done so already. Select Dropbox API app as shown below:

Fill out the rest of the form:


As you see in the screenshot above, you can't use the word "Dropbox" in the app name - the branding guidelines tell you that you can't do that. Once you successfully create an app, you get a dashboard similar to the one below. Find the app key and app secret, and copy and paste it into the right file in Eclipse.


4. Next, try running the project again. You see this:


It means we also need to change the manifest file of the app to include the app key. As you can see in the following screenshot, you replace "CHANGE_ME" with your app key in the line highlighted:


5. Run the project now. It should work! You'll see this:


Clicking on "Link with Dropbox" as seen in the screenshot above would prompt you (and anyone who uses this app) to sign in with Dropbox:


After going through the sign-in flow and granting permission for the app to access the files and folders in your Dropbox, the app brings you back to the main view. You can click on "New Photo" to upload a photo, and see a progress bar of it uploading. Clicking on "Roulette" will display a photo that was uploaded onto Dropbox.


That's it! It was super painless and easy to set up the starter project for the Dropbox Core API Android SDK .

Monday, September 1, 2014

Picasso Library

Ever wondered if there were an Android library that'd handle displaying photos for you in a beautiful grid, and most importantly, display it quickly?

I've been using Picasso by Square in a couple of my projects since the beginning of the year and I really like it.

One single line of code is all it takes to load an image hassle-free into your application:

Picasso.with(context).load("http://yourimage.com").into(imageView);
It is super speedy and you can load images not only from the web, but also from resources like R.drawable.yourimage and files like new File(...). But most importantly, it does all the heavy lifting for you so you just have to focus on writing your application code. Picasso takes care of caching, loading only the images you'd see, image compression, and all that good stuff.

Let's walk through how to set up the starter project that Square provides along with the Picasso library.

Install by downloading the .jar from https://github.com/square/picasso - click the red button under "Download" as shown in the screenshot below.


Let's set up the sample project from the GitHub repository and see the library in action for ourselves.

Once you import it into Eclipse as an Android Application Project, you should create a folder called "libs" and paste the picasso jar file from the step above into it.

Let's work on solving the import errors that comes with this sample code:


Really, it's annoying to have to deal with import errors every time you add a library. In this case, adding the Android Support Library will solve some of the problems.


But this doesn't solve everything; you've got to solve the rest of the import errors manually. 


Whenever you have errors with R, it means your resources folder ("res") has problems. Sure enough, it says that there is "no resource found that matches the given name: attr 'android:fontFamily'".



This is because android:fontFamily is only available to API level 16 and above. There are two solutions:

1. Check your API level, and target your build to API level 16 and above. Do this by right clicking on the project in the Package Explore view, going to Properties, and selecting Android.


You can easily see which API level you are currently compiling against, and you can change this to something above API level 16 by using the checkboxes.

2. Simply remove all references to android:fontFamily. For example, remove that line with the error in the code above. This is only a UI change to make the font pretty, and we can do without it. Go through the rest of the layout files and remove any such references. 


Notice that the sample_widget_info.xml file has similar problems as well. We get a similar error message: "error: No resource identifier found for attribute 'widgetCategory' in package 
 'android'". 


Again, this is because widget is not supported below API level 17, so you either remove widgets from the sample, or you target API level 17 and above.

If you choose to target a lower API level and delete the widget functionality, you have to make changes in other files as well, such as AndroidManifest.xml. Delete the segment below:



It's worth it because your library can now be used to target older versions of Android. Because of the fragmentation of the Android OS, a lot of people use older versions of Android and you don't want to limit your audience just because of a functionality in a library you used that may not turn out to be essential anyway.

Also delete SampleWidgetProvider.java and comb through the .java files, removing any Widget functionality.

This should resolve all the errors and you can run the sample app on your device or an emulator to see the Picasso library in action. Prepared to be amazed by how blazingly fast it is :)