Keep highlight selected item in Gridview Android


Gridview have option to modify it’s selector (listSelector). When we click on item in Gridview, we saw blink blue and then dissapear after we release it.

To make it stay, all we need just declare it at “android:listSelector” in GridView XML.
For instance :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calendar_gridview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:listSelector="@color/caldroid_sky_blue"
    android:adjustViewBounds="true"
    android:background="@color/caldroid_lighter_gray"
    android:gravity="center_horizontal"
    android:horizontalSpacing="1dp"
    android:numColumns="7"
    android:padding="1dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" />

To make selector background not overriden / set in bottom side, use “setDrawSelectorOnTop(false)” :

1
2
3
gridView = (GridView) inflater.inflate(R.layout.date_grid_fragment,
                container, false);
gridView.setDrawSelectorOnTop(false);

One response to “Keep highlight selected item in Gridview Android”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.