Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Recycler view item background color by code

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>

like image 344
Pankaj Bansal Avatar asked Aug 25 '16 12:08

Pankaj Bansal


2 Answers

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.

like image 144
Mahamadali Avatar answered Oct 05 '22 14:10

Mahamadali


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);
    }
}
like image 33
pawelo Avatar answered Oct 05 '22 16:10

pawelo