0 Replies - 425 Views - Last Post: 03 November 2018 - 03:52 PM

#1 AndroidGeek   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-November 18

Adding background color to cardview

Posted 03 November 2018 - 03:52 PM

I was trying to add color to a cardview in android. For that I was using view recycling. I wanted that my fourth item of the recycler view should have background of a drawable type and rest all to have background color from color.xml file. It loads correctly but when I scroll down and then up there are two list items having drawable background.
public class SelectedSiteRecyclerView extends RecyclerView.Adapter<SelectedSiteRecyclerView.MyViewHolder> {

    private Context mContext;
    private ArrayList<SingleGridViewHolder> mSelectedSitesList;

    public SelectedSiteRecyclerView(Context mContext, ArrayList<SingleGridViewHolder> mSelectedSitesList) {
        this.mContext = mContext;
        this.mSelectedSitesList = mSelectedSitesList;

    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.single_grid_view_layout, parent, false);
        return new MyViewHolder(view);

    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            //add drawable background
        }else {
            //add background color from color.xml file

    public int getItemCount() {
        return mSelectedSitesList.size();

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView mIconImage;
        private TextView mIconText;
        private TextView mNotificationText;
        private CardView mIconCardView;

        public MyViewHolder(View itemView) {
            this.mIconImage = itemView.findViewById(R.id.icon_image);
            this.mIconText = itemView.findViewById(R.id.icon_name);
            this.mNotificationText = itemView.findViewById(R.id.notification_text);
            this.mIconCardView = itemView.findViewById(R.id.icon_card_view);

            itemView.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Toast.makeText(mContext, "View Clicked", Toast.LENGTH_SHORT).show();


please help.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1