How to select an item in a ListView programmatically?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Programmatically selecting an item in a ListView is useful when restoring state, jumping to search results, or highlighting newly inserted data. On Android, the exact behavior depends on selection mode, adapter updates, and view lifecycle timing. A reliable implementation sets the selection after data is loaded and keeps selected state in the adapter model.
Basic Programmatic Selection in Android ListView
For simple single-choice lists, set choice mode and call selection methods.
setItemChecked controls checked state, while setSelection scrolls to the row.
Selecting After Async Data Load
If data arrives asynchronously, select only after adapter update is complete.
Posting to UI queue avoids selecting before layout pass finishes.
Select by Value Instead of Hardcoded Index
When list ordering can change, find index by value.
This is more robust for sorted or filtered lists.
Handling Custom Adapters
For custom row layouts, keep selected item ID or key in adapter state and update row background in getView. Relying only on visual row state can fail when rows are recycled.
Example adapter state idea:
- store
selectedId - call
notifyDataSetChangedafter selection change - style selected row based on current item ID equality
This preserves selection across scroll and view reuse.
Persisting Selection Across Rotation
Store selected position in instance state and restore it after adapter rebind.
Without restoration logic, selection can disappear after configuration changes.
ListView Versus RecyclerView
For new Android code, RecyclerView is generally preferred. The selection principles are similar, but state is typically managed in adapter or view model rather than view methods alone. If your project still uses ListView, keep selection management explicit and tested.
Accessibility Considerations
When selecting programmatically, ensure the user still has context. If selection changes due to background events, announce it with accessibility APIs or visible status text so focus changes are understandable.
Testing Selection Behavior
Automated UI tests should verify selection after list refresh, rotation, and filtering. Selection logic often passes manual tests but breaks when item order changes under dynamic data loads. A stable test matrix that covers empty lists, single-item lists, and long lists with scrolling helps catch state bugs early and prevents regressions when adapter code evolves.
Common Pitfalls
- Calling selection methods before adapter data is attached
- Using hardcoded indexes when list order changes dynamically
- Forgetting
CHOICE_MODE_SINGLEor multi-choice configuration - Relying on row view state without adapter-level selection state
- Losing selection after rotation because state was not restored
Most issues are timing and state-management problems rather than API limitations.
Summary
- Set selection after adapter data is ready and list is laid out.
- Use
setItemCheckedfor checked state andsetSelectionfor scroll. - Prefer value-based selection when order can change.
- Persist selected state across lifecycle events.
- Keep adapter state as source of truth for custom rows.

