Thursday, August 14, 2014

Lime ORM for Android

Hey friends. I wanted to let everyone know I've released a little library called Lime for Android. It's an ORM layer inspired by Microsoft's Entity Framework. I feel it has a few advantages over some of the other layers out there.

  • It's fast. Lime uses reflection to populate objects from cursors, but object inspection is done only once and cached. 
  • It's efficient. Lists of objects are backed by cursors so scrolling through huge data sets only requires conservative memory allocation. 
  • It's flexible. Most layers in Lime are exposed to make using as much or as little as necessary a simple matter of choice. 
  • It's familiar. Lime works with Android's content providers, observers, and loaders. It's all the things you already know, but easier. 
Stop by the Lime site on Bitbucket and take it for a spin. I think you'll dig it. 

Wednesday, July 9, 2014

Pull To Refresh on Android with SwipeRefreshLayout

In March of 2014, without much fanfare, Google added the SwipeRefreshLayout widget to the Android SDK. This addition formalizes the pull-to-refresh mechanism on Android. Let's take a look at how we'll use it.

First, make sure you have R19.1.0 or newer version of the Android Support Library in your project. R20 is the first version of the library to include the new layout. Once it's in your project, create a simple activity layout with the SwipeRefreshLayout at the top of the view hierarchy. While this isn't explicitly necessary, it makes sense that the entire activity be pulled down to refresh.

It is explicitly necessary that this layout have only one child. In this example, we'll make the only child a TextView for simplicity. Here's a look at our activity layout:

< xmlns:android="" android:id="@+id/layout" 

   <TextView android:id="@+id/text1" 
   android:text="@string/hello_world" />


Now that we have the layout added, we'll need a little code to make the refresh happen. The new layout has a method to setOnRefreshListener. So let's take a look at our activity with that listener connected:

public class MainActivity extends Activity { 

   SwipeRefreshLayout layout; 
   TextView text1; 

   OnRefreshListener refreshListener = new OnRefreshListener() { 
      @Override public void onRefresh() { 
         text1.setText(new Date().toString()); 

   @Override protected void onCreate(Bundle savedInstanceState) {
       layout = (SwipeRefreshLayout)findViewById(; 
       text1 = (TextView)findViewById(;
       layout.setColorSchemeResources(color.holo_blue_dark, color.holo_blue_light,color.holo_orange_dark, color.holo_green_light); 

Notice that we need to call layout.setRefreshing(false) when our refresh code is finished. This method stops the animation and let's the layout know that our work is done. Typically this would be called when a loader in the activity completes. 

Also, take a look at SwipeRefreshLayout.setColorSchemeResources. This method allows you to choose 4 colors to be in the animated bar while the layout is refreshing. 

SwipeRefreshLayout is a great addition to the SDK and goes a long way to standardizing a user interaction method. Implementing will give your users comfort that their app is acting in a standard fashion and also save you space on the ActionBar where your refresh button previously would have been!