Home » Android » android – how to set grid view item as selected

android – how to set grid view item as selected

Posted by: admin June 15, 2020 Leave a comment


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

How to&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);


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



In getView() method of adapter do the following

if(row != null) {
    if(position == selectedPosition){
        set required background color
    } else {
        set background color to Color.TRANSPARENT

In onItemClick method set Adapter.selectedPosition value and call notifydatasetchanged() and then proceed with your required logic.

in <GridView> item of xml set listSelector to android:color/transparent


Check with the following method


This will let you change the background of your selected item as the new image.

And if you want of change the color of the selected item then use-

GridView.setBackgroundColor(int color);

This will solve your problem.