6

I want to change the background color of my RecyclerView item. I think I should be able to do it in onBindViewHolder method but I am not able to do this. I only get bottom border color of the item changed but I want to change the full background color

Here is what I want

public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {

    if (//Some Condition) {
        holder.itemView.setBackgroundColor(Color.GREY);
    }
    else {
        holder.itemView.setBackgroundColor(Color.RED);
    }
}

I think this should produce something like

enter image description here

What I get is this

enter image description here

Here is my RecyclerView Fragment layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/installed_recyclerView"
        android:paddingTop="1dp"
        ></android.support.v7.widget.RecyclerView>
</LinearLayout>

Here is my item layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="3dp"
android:paddingBottom="1dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/post_card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="1dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Arial New"
            android:id="@+id/installed_font_name"
            android:textSize="16dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Monotype Solutions"
            android:textSize="12dp"
            android:id="@+id/installed_preview_company_name"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Preview Test"
            android:id="@+id/installed_preview_textview"
            android:textSize="30dp"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingRight="10dp"
                android:gravity="center"
                android:id="@+id/installed_preview_Unnstall">
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_remove_circle_24dp"
                    android:background="@android:color/transparent"
                    android:tint="@android:color/holo_red_light"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Uninstall"
                    android:textSize="12dp"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingRight="10dp"
                android:gravity="center"
                android:id="@+id/installed_preview_flip_layout"
                >

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_autorenew_24dp"
                    android:background="@android:color/transparent"
                    android:tint="@android:color/holo_blue_dark"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Flip This"
                    android:textSize="12dp"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

Pankaj Bansal
  • 889
  • 1
  • 12
  • 37

4 Answers4

6

Please try to change the code in onBindViewHolder as below

if(// Any Condition) {  
    ((CardView)holder.post_card_view).setCardBackgroundColor(Color.GREY);
}
else
{
     ((CardView)holder.post_card_view).setCardBackgroundColor(Color.RED);
}

I hope this will help you , Feel free to comment.

Mahamadali
  • 319
  • 3
  • 11
  • This answer could be better. ViewHolder pattern was introduced to get rid of using findViewById() and explicit casting when binding data. Those operations are considered expensive and don't need to be repeated on every onBindViewHolder() call – pawelo Aug 25 '16 at 17:37
  • I am not sure if it is better but there is a viewType: https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – JCarlosR Jul 29 '17 at 16:40
  • I don't think it is a good idea. I had been using this concept since a long time, but recently I faced a problem, ie, during very fast or slow scroll, the background color changed where it shouldn't have. So I manipulated views via getItemViewType(). Surprisingly, changing visibility of a view within a viewHolder in onBindViewHolder works perfectly well. I don't know what causes this abrupt behaviour. – Koushik Shom Choudhury Apr 18 '19 at 18:31
4

I think that holder.itemView is not CardView but LinearLayout which holds CardView. Try something like this:

public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {

    if (//Some Condition) {
        holder.yourCardView.setCardBackgroundColor(Color.GREY);
    }
    else {
        holder.yourCardView.setCardBackgroundColor(Color.RED);
    }
}
pawelo
  • 1,405
  • 4
  • 15
  • 30
2

@pankaj as @pawelo said casting can be costly sometimes so the best solution would be to give transparent color to CardView in xml like this

 <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/post_card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="1dp">

and then set background color to holder itemView like this.

 public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {

    if (//Some Condition) {
        holder.itemView.setBackgroundColor(Color.GREY);
    }
    else {
        holder.itemView.setBackgroundColor(Color.RED);
    }
}

now it should work as intended.

4b0
  • 21,981
  • 30
  • 95
  • 142
Velmurugan V
  • 428
  • 4
  • 12
0

I tried the above answers in my project, it worked but Android studio gave me errors. So I used holder.text_container.setBackgroundResource(R.color.category_numbers); instead and the red lines were gone. I used color from my custom resource. I hope this helps someone.

rugue
  • 23
  • 6