Reading time : Less than 10 Minutes
Perhaps one of the most usable widgets released by Google must be the newly added SwipeRefreshLayout. See, I’m not exaggerating but it is really useful. Before this solution was released, if you want to copy the Apple’s patented Pull-Down-To-Refresh UI pattern, you were left with your own implementation or some third-party libraries. Google didn’t appear to be much interested in this pattern either. Even Cyril Mottier had an article describing how Pull-to-refresh is An Anti UI Pattern on Android. Despite all these, a lot of developers were using this UI pattern. Like many others I was using it too. Anyway, now we have SwipeRefreshLayout, an awesome widget which can hold any scrollable views as a child. I have tested with Listviews and Scrollviews and it works smooth.
Now to the peace of mind, Google has included SwipeRefreshLayout in their Support-v4 library. You can use it from any platform level grater than 4. It is easy to use and easy to customise. In this article, I’m explaining a simple example application that does the same. First of all, you need to add the support library dependency to your Gradle script. If you are using other build systems, equivalent of the same is applicable there as well.
Once you add the above line, you need to sync Gradle once the system adds the necessary files to your project for this dependency. Now, ahead to the XML. It is fairly simple.
You must implemented SwipeRefreshLayout.OnRefreshListener in your Activity or Fragment. This interface will provide you a callback public void onRefresh(); which is triggered when the user drags the scrollable view and releases it. Check out the simplest version of my activity class. I have changed the color scheme for the SwipeRefreshLayout to make it more attractive. You don’t necessarily need to do this.
The result is pretty attractive. Take a look. Note that, you can do your network fetch or the list update process from inside the onRefresh(); It is called on the UI thread, so if you are doing network requests, make sure it is thread safe.
Reading time | Less than 10 Minutes
This is more or less a learning stage article. I’m learning iOS these days, and as usual I find it challenging. I’m not a very noteworthy developer anyway, but still the learning curve is not so steep. You need the right tools and right amount of passion for learning iOS development. Ofcourse you need a Mac, and XCode running to develop apps for the iOS or the OS-X platform. Check my other iOS article for environment setup.
What we are building?
We are building a very simple weather search application. You may search with a city name, and the app will show you the latest weather information provided by the awesome Open Weather Map API. Openweathermap is free to use for non-commercial purpose. Their API has JSON support, means we can retrieve data from their systems as JSON Object that typically looks something like the following. Please note that JSON is not particularly useful, unless we parse it to an NSDictionary or a NSArray.
Our app will let the user enter a city name and press a search button that triggers the network query. The GUI is simple, you will just need a Text Field, a Button and a Text View that fills the rest of the area on the screen. The text field will be used to display the result. Dealing with storyboard is easier in XCode’s Assistance View. Connect the Outlets and Action and you are done. Once correctly done, your XXViewController.h will look like the following.
Now it is the time for implementation. What we basically do is, create a NSURLConnection and make an asynchronous request with it. Once the response is received, which is by the way a JSON object, parse it to an NSDictionary. NSDictionary is further useful if you want to get the value of a particular key in the JSON or for doing such things. The implementation is as follows.
The result is just awesome! You enter a city name, and what you get is the complete response displayed in the textview. It is not the world’s most attractive UI, but it is simple and easy get started with. Take a look.
Hope this is useful to somebody. Happy Hacking!