Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set grid view item as selected

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

like image 413
user2702700 Avatar asked Sep 02 '13 18:09

user2702700


3 Answers

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);
like image 176
user3614314 Avatar answered Nov 07 '22 03:11

user3614314


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.

like image 23
Jerry Brady Avatar answered Nov 07 '22 02:11

Jerry Brady


I hope this will help you

 gridView.requestFocusFromTouch();  
       gridView.setSelection(1);
like image 3
Bebin T.N Avatar answered Nov 07 '22 03:11

Bebin T.N