Search code examples
androidgridviewhighlightmultiple-selectmultipleselection

How to implement multi item selection in a GridView with ImageView changind color to blue highlight?


I would like to implement multi item selection in a GridView with ImageView changing color to blue. I mean I have a GridView with ImageView where I load user's image from url. In my GridView I would like to highlight the multiple selection image (es blue) like in picture

enter image description here

My GridView :

   <GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="3"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="vertical"
    android:listSelector="@null" />

Imem in a GridView:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/userLikesimg"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:background="@color/blu_facebook_transparent"
  android:scaleType="centerCrop" />

Solution

  • I solved unisng LayerDrawable in my adapter:

    numElement is int and define num elements chosen

    selectedElements is array of boolean with position elements chosen

    public boolean[] selectedElements= new boolean[n];
        private int selectedElements= m;
    
     public View getView(final int position, View amico, ViewGroup parent) {
          ...
          viewHolder.userImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectedElements[position] == false) {
                        if (numElementi == 0) {
                            Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show();
                        } else {
                            BitmapDrawable bd = new BitmapDrawable(mContext.getResources(), viewHolder.userImage.getDrawingCache())userUrl;
                            //you can check if the bd is null (if it is null I download image again -it is in cache- and I add blue trasparency like bellow                                 
                            //R.drawable.blue is image with trasparency
                            LayerDrawable d = new LayerDrawable(new Drawable[] { bd, mContext.getResources().getDrawable(R.drawable.blue) });
                            viewHolder.userImage.setImageDrawable(d);
                            selectedElements[position] = true;
                            numElementi--;
                        }
    
                    } else {
                        String imgUserurl = userUrl;
                        Picasso.with(mContext).load(imgUserurl).placeholder(R.drawable.ll_friend_placeholder).into(viewHolder.userImage);
                        viewHolder.userImage.setDrawingCacheEnabled(true);
                        selectedElements[position] = false;
                        numElementi++;
                    }
                }
            });
          ...
      }
    
      public boolean[] getSelectedElements()
      {
         return selectedElements;
      }
    

    In my activity or fragment:

        boolean[] selectedElement = adapter.getSelectedElements();
    

    (I need position of selected element)