Android ListView – Fixing Missing/Blank Dividers

Putting up a list of elements that people can see and work with is a very common tactic when working on mobile applications.

It is something that developers are called upon to do all the time, and it is a good thing we have them working on these things because otherwise the apps that we all use on a regular basis simply wouldn’t hold up to the workout that we put them through when using them. That is to say that they wouldn’t look nearly as good as we are used to them looking at this point.

Most developers will look at ListView in Android at some point in their developing career. It is a typical thing for developers to do, and you might as well get used to using this application if you intend on working in the field for very long. It will come to be expected of you.

A Full View Of Information

A primary reason to use ListViewer when developing a new app is simply to allow users of that app to see everything that they need to see as they are scrolling through it. They may want to spend some extra time on certain elements of what you have created, and that is all possible when you provide them with the list or “table of contents” for your application.

You may be surprised to learn just how much people appreciate this and how much it truly benefits them. Funny as it might sound, these are the types of things that make a difference in the lives of people who are just trying to figure out what kind of apps they will use.

The ListViewer app allows developers to provide their users with exactly what they are looking for as far as the information they need to access right when they need to access it. No more hunting around trying to find a specific aspect of the app that you are currently using. It is all right there when the developer has taken the time to use ListViewer to make it easier to scan.

Accessibility is the Name of the Game

Have you ever used an app that you weren’t quite sure how to find everything in? There are many people who have put themselves in this unfortunate predicament before, and the results can be devastating. That is to say that they are not app with the outcome because they end up getting lost in the maze of the app and are unable to truly enjoy its benefits.

Creating lists within the app makes it a lot easier for anyone to take a look at what is going on within the app and try to figure out which steps they need to take next to get to the information that they actually desire.

As you might imagine, this is a very big deal when working with apps that contain valuable information within them. If you are working with something like that, then you definitely want to get to the root of what you are looking for right away.

The ability for users to discover what they are looking for always begins at the development stage. When developers are working to create apps that people will actually use, they need to think carefully about the way that the average user is likely to approach the app. How is that user going to view the material that is easy in the app, and where are they most likely to click? Those are the type of questions that developers should always ask themselves if they are serious about creating something that is going to stand the test of time.

A post with code in it. Because it’s been a long time since the last one.

If you work with ListViews in Android, as all Android developers will do at some point, you may notice that if you set your list items to be non-selectable the dividers that are drawn between each cell disappear. In the Holo Light theme you would normally get a thin light gray dividing line between cells.

Depending on how you’ve implemented your adapter, you may find that these dividers become white/transparent when the cells are not selectable. According to Android framework engineer Romain Guy, this is the intended behavior.

As Haythem Souissi points out in this Stack Overflow answer, you can work around this by ensuring that the areAllItemsEnabled method returns true, even though all items are not enabled (maybe none of them are). The isEnabled method will take care of actually disabling the cells and the dividers will be drawn between each of them.

All very straightforward so far but it all goes wrong again when you try to add a non-selectable header view to the list. The way that ListView deals with headers and footers is that it creates its own adapter to wrap/decorate yours and insert the header/footer views in the appropriate places. This is fine but it’s not delegating the areAllItemsEnabled method, so our above fix no longer works.

Fortunately, and unusually for Android, everything we need to resolve this issue is part of the public API. The adapter decorator class is android.widget.HeaderViewListAdapter. We just need to create our own instance, wrapping our own adapter, and override areAllItemsEnabled as above.

There’s a slight complication in that we have to wrap the header view in an instance of ListView.FixedViewInfo and this is a non-static inner class of ListView, but we can reach into our bag of obscure Java tricks to create an instance from outside the enclosing class.

// I assume you know how get a reference to the ListView and create your own adapter.
ListView listView = (ListView) view.findViewById(android.R.id.list);
CustomAdapter adapter = new CustomAdapter(context, listOfItems);

// You can create any view you like for the header.
TextView listHeader = (TextView) inflater.inflate(R.layout.list_header, null);
listHeader.setText("My Header Text");

// This is how you create an instance of the non-static nested class, providing
// it with a reference to an instance of the containing class (ListView).
ListView.FixedViewInfo headerInfo = listView.new FixedViewInfo();

headerInfo.view = listHeader;
headerInfo.isSelectable = false;

// HeaderViewListAdapter insists on concrete ArrayLists.
ArrayList headers = new ArrayList(1);
headerInfoList.add(headerInfo);
ArrayList footers = new ArrayList(0);

HeaderViewListAdapter wrapper = new HeaderViewListAdapter(headers, footers, adapter)
{
    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }
};
listView.setAdapter(wrapper);

Create Something Unique for Android?

Here is another thing that people sometimes forget about. They may want to consider creating something unique for the Android system. There are plenty of Android users out there, and it is a great platform on which to launch your application to get it started.

If you discover that the ListViewer and every other piece of code you have placed into your app works out well on the Android platform, then there is a good chance that it will do great on other platforms as well. Thus, you may bring it over to those other platforms after you have given it a chance on the Android system.

See what Android users love and hate about what you have created, and then fix the problems and move on to the next challenge. You may find that you have created something that users truly love and that you can market to other users on different operating devices (such as Apple iOS).

The best thing to do is to figure out the issues that you may need to clean up while putting your material out to a large audience. The Android platform is a large enough audience that you should be in great shape if you begin there. They are also helpful in that they will provide you with the feedback that you may need to make your app just right.

Don’t Create Too Many Lists

Finally, we leave you with the reminder that you shouldn’t create too many lists within your app. Yes, you want people to come and check out what you are all about, but you do not want to give them the impression that they have to continue to click through sub-list after sub-list just to get to the materials that they are actually attempting to view.

If you put them in a position like that, then they may feel that you are making things too challenging for them, and this is likely to upset some users. Get away from that by only using lists when they are truly necessary within your app. Otherwise, try to cut them out and keep a clean and crisp look to what you have created.

At the end of the day, users want two things. The first is that they want an app that can help them solve whatever issue your app seeks to solve. The second is an app that has a user-friendly and clean look to it.

You can give them both of those things if you just work with what you have created to make it as smooth and seamless as possible. It is not as challenging as you may at first believe. Give it a try, and discover a whole new world of possibilities.

.

Leave a Comment