Layouts / ListView / RecyclerView

Tool Time – Part 1

A common use-case when we’re designing a layout containing a RecyclerView (or, if you’re a glutton for punishment: ListView) is that we may have to display data that is either generated or obtained at runtime. For example, we retrieve the data for the list items from a cloud API. This can make it difficult to properly visualise the data during development so often we’ll find ourselves having to fire up the app to obtain real data each time we make a minor tweak to the item layout. There are actually some neat helpers build in to the tools: namespace which can help us quite considerably. In this short series we’ll take a look at these tools and see how they can speed up our layout development.

The first problem that we have when working with either ListView or RecyclerView is that it can be somewhat difficult to visualise what our populated list is actually going to look like.

Let’s start by looking at the layout that we’ll use for each item within the RecyclerView. For our example we’ll consider a list of people. Each item in the list will contain a picture of the user, the city they live in, the date they joined a service, and some longer description text. The layout for such an item may be something similar to this:

The big problem with this is that when we view this in the layout Design or Preview panels in Android Studio it looks like this:

Although the layout Design editor shows us the outlines of some of the views, if we look at the preview of our RecyclerView, then it really gives us no clue whatsoever what our populated RecyclerView will look like because a simple placeholder has been used for each item:

However there’s a little trick that we can use which will enable us to use the actual item layout in the preview:

The highlighted line tells Android Studio to use the correct layout when displaying the preview:

We can see a little more of the structure of each item, but it really doesn’t help us to visualise how the complete list will look.

Many people will already be familiar with how we can use the tools:text="..." attribute on a TextView to include some text for the preview:

If we do this throughout, the preview begins to look a lot nicer:

We could do a similar trick to add a placeholder avatar image (by using tools:src"...") but then we might need to add the placeholder image to our resources which could bloat our APK so we should really add it only to the resources for debug builds to keep our APK clean. However there is actually a better way. In Android Studio 3.0 and later we now have some sample data which we can add without having to add the resources ourselves:

Using the avatars item from the sample resource will add a dummy avatar image for us. Not only that, but it will add a different image to each item. We can do the same thing with all of the text, as well. There are a number of sample text items and this fit well with our chosen user data:

Now our layout preview begins to look a lot more realistic and gives us a much better idea of how the final layout will look:

Moreover, the sample data does not get added to our codebase so there’s no risk of accidentally bloating our APK. Also by using the sample lorem ipsum text in the description field we can check that our three line limit and ellipsis behaviour is working correctly in the preview.

Another thing worth noting is that Android Studio is doing all of this without using the RecyclerView.Adapter. The actual Adapter implementation will not actually populate anything. It is important to understand this so that you know that you can test your layout in isolation from your Adapter, and also know that no verification or validation of the Adapter itself is being performed using this technique.

A full list of the available types of sample data is in the official documentation.

As the more astute will have noticed, I deliberately chose an example of user info what works well with the available type of sample data. However, if the required data isn’t provided in the sample data, there is a way of using custom data instead and we’ll look at that in the concluding article in this series.

The source code for this article is available here.

© 2018, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Tool Time – Part 1 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *