I have a GridView
which uses array adapter to populate the data.
By default when the GridView
is displayed I have to show one of the item as selected. I have used gridview.setselected()
, but it didn't show the GridView
item as selected ( I mean the background of the item is not changed). is there any way to show a specific item as selected when grid view is loaded. Please suggest
I struggled with this a bit yesterday before realizing that what I actually wanted was for the item to be checked. i.e.
gridview.setItemChecked(position, true);
GridView.setSelected()
is actually inherited from View
and just sets the entire GridView
in your layout as selected.
What you are looking for is GridView.setSelection(int position)
to select the current item in the GridView
starting from position zero.
Another useful function is GridView.getFirstVisblePostion()
which can use to store the current location on the GridView
(if it's large and scrollable) so that it can be restored when you are restored from instance state (say on a rotation) or otherwise.
When working on such things, it's always useful to look through the Android Developer documentation for your widget/object.
I hope this will help you
gridView.requestFocusFromTouch();
gridView.setSelection(1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With