Pages

Saturday, June 22, 2013

A Volley-based Networking Framework for Android Apps: Part 1

Hi guys! Hope you found my previous post about applying custom typefaces to your TextViews useful. This post will be the first in a three-part series on making your networking calls concurrent, fast and easy. Today's topic will be an introduction to a networking toolkit introduced at Google I/O 2013: Volley.

An Introduction

Volley is a networking toolkit built by +Ficus Kirkpatrick  and team(?) for the Google Play Store app. It's well architected, easy to understand and extend, and best of all, it's built on top of a fluent interface(Yes, I love fluent interfaces.. Who doesn't?).

If you haven't already watched the video, you really should. Personally, it was one of my favourite talks from this year's Google I/O sessions.


Off the top of my head, here's a quick list of some of its best features

  • Concurrent
  • Works well with the Activity/Fragment lifecycle(this is a HUGE plus, in my opinion)
  • Handles Image Loading
  • Switches between the Apache HttpClient and HttpUrlConnection automatically based on the device OS(for those who don't know, Android 2.2 has bugs with the HttpUrlConnection)
  • Has a Disk-based cache for the network responses, which returns the cached response based on the HTTP Response headers
  • Has an awesome request cancellation policy

Setup

The first thing you need to do is to create an instance of RequestQueue and ImageLoader(if needed). I find the best place to do this is in the onCreate() of a custom Application Class and then hand them over to the Activities and Fragments as required.


The BitmapLruCache is the same as the one mentioned in the Android developer guide for Caching Bitmaps. It just needs to implement the ImageCache interface of ImageLoader.

Usage


Talking with APIs

Now, we get to the fun part. Before we make requests, we should initialize two Listeners, one for a successful response and one for an error response.


Once you've done this, making an API call is as simple as

Volley will pick up requests from the Queue and execute them. Your Listener methods will get called automatically when the request finishes. You can queue up as many requests as you like. In case your Activity/Fragment gets stopped before any queued requests complete, Volley can cancel any pending requests from that Activity/Fragment and also prevent responses getting delivered to the stopped Activity/Fragment. All you need to do is override the onStop() method in your Activity/Fragment.


Voila! No more null checks in your Listener code!


Image Loading

Image Loading was a right pain until now, what with caching and rescaling Bitmaps and recycling of views in AdapterViews. Volley takes care of all that. You can use Volley's ImageRequest, but you'll still have to cancel it when recycling Views in AdapterViews. What you can do instead is use their NetworkImageView widget in your layouts.


All you need to do in your code then is


That's all for today. Hope you liked it! Part 2 will talk about how we modified and extended Volley for our Networking Framework. For a sneak peek at some of the modifications, check out Enhanced Volley, which is my fork of Volley including some of my enhancements. I plan to keep it up to date with the enhancements and fixes made to Volley as well.

Happy coding!!





Saturday, May 11, 2013

Using Custom Fonts with Android TextViews

It's been about a year since I started Android development. While the Android framework is powerful, there are some things that should not be so difficult, like applying custom fonts to a TextView. I've been playing around with this for a while, and I've finally got a good framework in place that will really simplify applying custom fonts to TextViews. This article is meant for intermediate Android developers, who are tired of typing in font names. It'll also be the most beneficial to those who use Eclipse and want to take advantage of its Auto-Complete features to automatically suggest the fonts.

Ready? Let's get started!

Step 1: Adding your fonts

First of all, you'll need to add the fonts you want to use to your project. As an example, we'll use Google's own Roboto fonts, which come included with the Android SDK. You'll find them in your Android SDK directory under the platforms/android-15/data/fonts folder. Copy the fonts you want into the assets/fonts folder of your project directory. Let's also add the Ubuntu Fonts. You can download them for free.

Step 2: Writing your Custom TextView

You will need to make a custom TextView extending TextView. Add the following TextView in your source directory.

A little explanation about how this works. The styledAttrs object is an array of attributes which are passed to the Widget in the XML Layout. From these, we extract the one important to us(more on that later) and use it to create the Typeface we need. After that, we call setTypeface() to add the typeface to the TextView.

Step 3: Adding a TypefaceCache

Strictly, this is not necessary. But we will do this for a very important reason: performance! Creating a Typeface from the assets is a heavy operation and we'll probably be using these fonts in ListViews. If we don't cache the typefaces, the performance of the ListView will degrade horribly. This way, we only create the Typeface once, and recall it from the cache when needed.


Step 4: Declaring a custom attribute

This is where the fun stuff starts! We want to make it really friendly for the developer to use this widget, so we declare an attribute "fontStyle" in XML, which will be an enum. Each enum value will map to a particular font in the TypefaceCache. With this, Eclipse will automatically suggest the fonts to you when you begin typing in the attribute name, like it does for the default attributes. Add the following to your res/values/attrs.xml file


You will probably need to restart Eclipse so that it can build its auto-suggestion cache when adding these attributes.

Step 5: Tying it all together!

The first thing that needs to be done is adding your app's namespace to the XML layouts where you're going to use the TypefacedTextView. Add the following line below the xmlns:android declaration in your layout file.



Then you can use your TypefacedTextView just like a normal TextView but with a new attribute - fontStyle.

If you are running the newest version of ADT, you won't have to add the namespaces manually, as Eclipse will automatically fill it in for you!


Run your application and Huzzah! It's alive!


That's all for now! Hope you liked it!