Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView Highlight selected Item

I am using custom background for Grid items, and as normal it works, but when i do click on any of the Grid item (it's not working)

So what could be the reason, why its not working when i am doing click on any of the Grid Item, whereas its working in normal mode..

For ImageGallery I am using this tutorial and to highlight grid item following this one

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/blue" android:state_pressed="true" />
    <item android:drawable="@color/blue" android:state_selected="true" />
    <item android:drawable="@color/blue" android:state_focused="true" />
    <item android:drawable="@color/white" />

</selector>

Grid item layout xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/grid_background"
     android:descendantFocusability="blocksDescendants"
     android:padding="5dp">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scaleType="centerCrop">
    </ImageView>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="1dip" >

        <ProgressBar
            android:id="@+id/loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />

    </FrameLayout>

</RelativeLayout>

Here is the main grid xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <!-- The GridView to display picture's preview  -->
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:gravity="center"
        android:stretchMode="columnWidth"    
        android:drawSelectorOnTop="true"       
        android:focusable="true"
        android:clickable="true"
        android:scrollbars="none"
        > 
    </GridView>

</RelativeLayout>

This is what i am getting as normal and even when i do tap on any of the GridView Item:

enter image description here

like image 534
Oreo Avatar asked Apr 28 '15 09:04

Oreo


2 Answers

This may help you make your selector like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/black" android:state_pressed="true" />
    <item android:drawable="@color/black" android:state_selected="true" />
    <item android:drawable="@color/black" android:state_focused="true" />
    <item android:drawable="@color/white" />

</selector>

and Relative layout add

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      android:clickable="true"
     android:descendantFocusability="blocksDescendants" 
     android:background="@drawable/grid_background"
     android:padding="5dp">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:focusable="false"
        android:scaleType="centerCrop">
    </ImageView>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:padding="1dip" >

        <ProgressBar
            android:focusable="false"
            android:id="@+id/loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />

    </FrameLayout>

</RelativeLayout>
like image 165
N J Avatar answered Nov 15 '22 08:11

N J


Write drawable file for your selector like this call it selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/blue" />
        </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</selector>

then inside xml of gridview add following two lines

 android:drawSelectorOnTop="true"
 android:listSelector="@drawable/selector"
like image 43
amodkanthe Avatar answered Nov 15 '22 08:11

amodkanthe