Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:To set an item as selected when the ListView opens?

An activity has a Button and a ListView. Initially, only the Button is visible. When the button is pressed, the ListView is displayed. When displayed, is it possible for me to show one particular item as selected/focussed?

A use case could be that suppose it is a list of language settings and when the list opens, the currently selected language must be shown as highlighted.

If I know the index of the item, how to set it as focused on display?

like image 677
kiki Avatar asked Jan 20 '26 13:01

kiki


2 Answers

I post my solution, because google still doesn't know the answer.

getListView().setItemChecked(selectedGroupIndex, true);
like image 111
Piotr L. Avatar answered Jan 22 '26 02:01

Piotr L.


In short, ListView::setSelection(int position) is what you need. However, depending on whether the device is in touch mode or not, it may or may not have visual effect (background highlighting). For more details, refer to Android ListView Selection Problem

like image 42
onlygo Avatar answered Jan 22 '26 01:01

onlygo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!