sdasdadas
sdasdadas

Reputation: 25096

How do I ensure change the background of a selected GridView cell?

In Android, I have a GridView - each cell in the GridView is an ImageView. When a user clicks on a cell, I would like that cell to be "selected" (have its background turn blue), and all other cells to "unselect" (have their backgrounds turn white).

I have implemented the following background drawable, but it only changes the background while the cell is pressed:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
       android:drawable="@drawable/iconborder_selected" /> <!-- pressed -->
 <item android:drawable="@drawable/iconborder_unselected" /> <!-- default -->
</selector>

EDIT: Here is my adapter code for the GridView.

class IconAdapter extends BaseAdapter {

    private Context context = null;
    private List<Drawable> icons = new ArrayList<Drawable>();

    public IconAdapter(Context context) {
        this.context = context;

        for (Field f : R.drawable.class.getFields()) {
            String path = f.getName();
            if (path.contains("icon_")) {
                int id = context.getResources().getIdentifier(path, "drawable",
                        context.getPackageName());
                Drawable drawable = context.getResources().getDrawable(id);
                icons.add(drawable);
            }
        }
    }

    @Override
    public int getCount() {
        return icons.size();
    }

    @Override
    public Object getItem(int position) {
        return icons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv = new ImageView(context);
        iv.setImageDrawable(icons.get(position));
            iv.setBackgroundResource(R.drawable.iconborder);
        return iv;
    }

}

Upvotes: 1

Views: 2217

Answers (2)

Serj Lotutovici
Serj Lotutovici

Reputation: 4380

Try in the XML declaration of the GridView to put these lines:

<GridView
 <!-- Some stuff here, like id, width, e.t.c. -->
 android:drawSelectorOnTop="true"
 android:listSelector="path_to_your_selector"
/>

And your selector should contain something like this:

<item android:state_pressed="true">
    <shape>
        <!-- Or a drawable here -->
    </shape>
</item>
<item android:state_focused="true">
    <shape>
        <!-- Or a drawable here -->
    </shape>
</item>
<item android:drawable="@android:color/transparent" />

Upvotes: 2

a.bertucci
a.bertucci

Reputation: 12142

You should add state_selected too:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true"
       android:drawable="@drawable/iconborder_selected" /> <!-- selected -->

  <item android:state_pressed="true"
       android:drawable="@drawable/iconborder_pressed" /> <!-- pressed -->

  <item android:state_pressed="false"
       android:drawable="@drawable/iconborder_unselected" /> <!-- default -->
</selector>

Upvotes: 2

Related Questions