0

Hello I build gridView gallery inside Dialog. I display some photos with checkboxes. Now I want check/uncheck checkbox after imageView click. For now it only checks when I click exactly on it.

here some code :

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null)
        {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.my_grid, parent, false);
            holder=new ViewHolder();
                holder.imageView=(ImageView)convertView.findViewById(R.id.imageView);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();

        holder.checkBox.setOnCheckedChangeListener(null);
        holder.checkBox.setChecked(check[position]);


        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int counter=0;
                for(int i=0;i<check.length;i++)
                    if(check[i])  counter++;
                numberCheckboxes=counter;
                if (isChecked && numberCheckboxes<=2)
                {
                    numberCheckboxes++;
                    check[position] = true;
                    Toast.makeText(mContext, mContext.getResources().getString(R.string.zaznaczono_zdj), Toast.LENGTH_SHORT).show();
                }
                else if (!isChecked&&position<check.length)
                {
                        numberCheckboxes--;
                    check[position]=false;
                }
               if (numberCheckboxes>=4&&isChecked)
                    {
                       buttonView.setChecked(false);
                        numberCheckboxes--;
                        check[position]=false;
                    }
                }


        });

        Glide.with(mContext)
                .load(this.listFiles.get(position).getAbsolutePath()) //path to picture
                .into(holder.imageView);
        return convertView;
    }
static class ViewHolder{
    ImageView imageView;
    CheckBox checkBox;
}

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:orientation="vertical">

    <ImageView

        android:layout_width="@integer/width"
        android:layout_height="@integer/height"
        android:adjustViewBounds="true"
        android:id="@+id/imageView"
        android:layout_margin="5dp"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="2dp"
        android:focusable="false"
        android:scaleX="1.8"
        android:scaleY="1.8" />

</RelativeLayout>

1 Answer 1

-1

Use :

convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.checkBox.performClick();
        }
    });
5
  • When I do that toast is shown but there are no animation for checked box, quite strange
    – user8207105
    Commented Jul 3, 2017 at 12:53
  • Where did you write code for checkbox animation ? I can't see
    – Piyush
    Commented Jul 3, 2017 at 12:56
  • additionally it always check first checkbox even If i click on another one
    – user8207105
    Commented Jul 3, 2017 at 12:57
  • You should check your condition for checkbox check uncheck
    – Piyush
    Commented Jul 3, 2017 at 12:57
  • I mean custom animation when I click nothing happen to checkbox just toast is shown that checkbox is checked but I don't see that
    – user8207105
    Commented Jul 3, 2017 at 12:57