Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Keeping the ListView with the User's Focus

Author: Ian Darwin
Published? true -- FormatLanguage: W

Problem:

You don't want to distract the user by moving the ListView to its beginning, away from what the user just did.

Solution:

Keep track of the last thing you did in the List, and move the view there in onCreate().

Discussion:

One of my favorite peeves is List-based applications that are always going back to the top of the list. To name a few examples:

  • the OpenIntents File Manager, when you delete an item from the bottom of a long list, goes back to the top of the list to redisplay it, ignoring the fact that if I deleted an item, I may be cleaning up, and would like to keep working in the same area;
  • the standard Contacts manager, when you edit an item, forgets about it and goes back to the top of the list;
  • several other List programs have this behaviour.

A similar error occurs in the HTC SenseUI for Tablets' mail program. When you select a bunch of emails using the per-message checkboxes and then delete them, the scrolling list is left in its previous position, which is now occupied by mail from yesterday or the day before!

It's actually pretty simple to set the focus. Just find the item's index in the Adapter (possibly using theList.getAdapter() if needed), then call

theList.setSelection(index);

This will scroll to the given item, and also select it so it becomes the default to act upon, though it doesn't invoke the action associated with the item.

No records found.