Search code examples
javaandroidandroid-imageview

Android ImageView Select Mark


There are 10 images in my application. User has to choose one of them. I want it to be tick mark in the upper right corner of the clicked image and not in others. How can I do that?


Solution

  • You can put your images in a recyclerview and maintain selected image's state with the following adapter

    ImageAdapter

    public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
    
        private Context context;
        private int selectedPos = -1;
    
        public ImageAdapter(Context context) {
            this.context = context;
        }
    
        public void setSelectedPos(int pos) {
            selectedPos = pos;
            notifyDataSetChanged();
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(
                    LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_image, parent, false)
            );
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            if (position == selectedPos) {
                holder.ivCheck.setVisibility(View.VISIBLE);
            } else {
                holder.ivCheck.setVisibility(View.GONE);
            }
        }
    
        @Override
        public int getItemCount() {
            return 10;
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            private ImageView ivCheck;
            private ImageView image;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                image = itemView.findViewById(R.id.iv_src);
                ivCheck = itemView.findViewById(R.id.iv_check);
    
                image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setSelectedPos(getAdapterPosition());
                    }
                });
            }
        }
    }
    

    layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
    
        <ImageView
            android:id="@+id/iv_src"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/colorPrimary"/>
    
        <ImageView
            android:id="@+id/iv_check"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:src="@drawable/ic_action_tick"/>
    
    </RelativeLayout>