phogel
phogel

Reputation: 2035

Android ListView programmatic selection/highlight

I have a simple listview and listadapter setup as follows:

listAdapter = new ArrayAdapter<MyDomainObject>(this, android.R.layout.simple_list_item_1, listOfDomainObjects);
listView.setAdapter(listAdapter);

The user makes a selection on the list which takes them to another activity. From the new activity they can click their selection which returns them to the activity with the above list. I want to highlight the previous selection made. I currently find the matching list entry and call:

listView.setSelection(matchIndex);

This brings their previous selection to the top of the list. Is it possible to highlight (in that default orange) the previous selection. I have tried several approaches with no luck.

Upvotes: 4

Views: 2444

Answers (2)

Amir Dora.
Amir Dora.

Reputation: 2707

use simple_list_item_activated_1 you're using simple one.

Also to highlight selected row use this code

listview.setItemChecked(position, true);

position refers to row id you want to select.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006594

What you have is fine. However, the determination of whether the selection is "highlighted" is determined on whether the user was using the touchscreen. If they have used the touchscreen more recently than the trackball/D-pad/whatever, the device is in "touch mode" and selection highlights are not shown.

Upvotes: 2

Related Questions