Question

Working with a GridView and trying to have the selected item highlight in grey. This works fine until I tested it on Android 2.3. On Android 2.3, this is what happens when you select an item:

GridView with selection

In this screenshot I had tapped on the Restaurants item. Essentially the entire GridView gets highlighted in grey and not the selected item. However, in Android 4.0+ this GridView works as expected and tapping on items correctly highlights them (not the GridView!) in grey.

Here is the relavent part of my layout.xml:

<FrameLayout
    android:id="@+id/grid_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView 
        android:id="@+id/categories_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:listSelector="@drawable/list_view_selector"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />
</FrameLayout>

And here is res/drawable/list_view_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@color/light_grey" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@color/light_grey" />
    <item 
        android:drawable="@color/transparent" />
</selector>

I've tried this workaround, but it just results in nothing getting highlighted at all (not even the selected item) on both 2.3 and 4.0+ devices.

Any help is appreciated.

Was it helpful?

Solution

Apply the selector to the background of the item layout instead and set android:listSelector="@null".

Grid:

<FrameLayout
    android:id="@+id/grid_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView 
        android:id="@+id/categories_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:listSelector="@null"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />
</FrameLayout>

Item:

<FrameLayout
    android:id="@+id/grid_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_view_selector">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

OTHER TIPS

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/calendergrid"
        android:layout_width="42dp"
        android:layout_height="45dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textStyle="bold"
         android:background="@drawable/listselectorforgridview"
        >
</TextView>

Just paste that and use like...

ArrayAdapter<String> dateadapter = new ArrayAdapter<String>(context,
                R.layout.textviewforcalendergrid, calenderdays);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top