TonyChou
TonyChou

Reputation: 113

android RecyclerView BackgroundColor

When I use RecyclerView to show list data in my app, the RecyclerView's backgroundcolor always been white:

screenshot

I set the backgroundcolor in xml file and code, but it does not work:

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:background="@color/colorPrimary">
    <!--<include layout="@layout/content_main" />-->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

//fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:padding="5dp" />
</RelativeLayout>

//item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="@color/yellow"
    card_view:cardCornerRadius="1dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            android:gravity="center"
            android:textColor="@color/text_white"
            android:textSize="@dimen/list_item_text_size" />

        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:scaleType="centerCrop" />
    </RelativeLayout>

</android.support.v7.widget.CardView>


    //gradle file
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.union.fmdouban"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

Upvotes: 2

Views: 34986

Answers (4)

Sai Chakradhar
Sai Chakradhar

Reputation: 1

This worked for me. I was getting booked slot ids and I want to show them as red so that they cannot book them.

private void populateItemRows(ItemViewHolder viewHolder, int position) {
  viewHolder.slotTv.setText("Slot " + position);

  if (bookedList.contains(viewHolder.getAdapterPosition())) {           
    viewHolder.slotTv.setBackgroundColor(Color.parseColor("#FF0000"));
  } else {
     viewHolder.slotTv.setBackgroundColor(Color.parseColor("#32cd32"));
  }
}

Example of how this works

Upvotes: 0

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

i have implemented the below code and this is showing the card view and background colorenter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/appointment_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:scrollIndicators="none" />
</RelativeLayout>

//Your adapter's layout

  <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:padding="5dp">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            android:gravity="center"
            android:text="dfgjkdfh"
            android:textColor="#ffffff" />

        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:scaleType="centerCrop" />
    </RelativeLayout>

</android.support.v7.widget.CardView>

I removed the main Relative layout from adapter's XML Try this and let me know

//My Gradle looks like this

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "betasoft.com.tryso"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.0'
    compile 'com.android.support:design:23.1.1' }

Upvotes: 6

tbrams
tbrams

Reputation: 101

I was having a hard time with this as well, until it dawned on me that I needed to get my hands on the rootView of the RecyclerView to stop my app from crashing when I tried setting a background color as a warning on my list.

The trick was apparently to do it all from the ListAdapter. Getting hold of a reference to the view can for example be done by overwriting the onAttached method in the RecyclerView.Adapter.

...
private static View mRootView;

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

    mRootView = recyclerView.getRootView();
}

Then I created a method for updating the background color using a trick I found on these boards like this

public static void updateBackgroundColor() {
    if (mIsThisDbMaintenance()) {
      mRootView.getBackground().setColorFilter(Color.parseColor("#AA0000"), PorterDuff.Mode.DARKEN);
    } else {
        if (mRootView.getBackground()!=null)
            mRootView.getBackground().clearColorFilter();
    }
}

This method is public static, so that I could use it from the menu option handler in another activity as well.

screenshot

Upvotes: 3

Mihir Joshi
Mihir Joshi

Reputation: 162

You can also set background color with condition in RecyclerView. Just in your RecyclerViewAdapter, add following line in onBindViewHolder method:

mRecyclerViewHolder.mRecyclerViewRowLayout.setBackgroundColor(condition.isRight() ? 0xFFAED581 : 0xFFE57373);

In case if you wondering what's arguments in SetBackgroundColor, here is it:

condition is just normal Boolean condition.

0xFFAED581 is just hex color code with 0xFF prefix(Programmatically, we need to use color code with 0xFF prefix instead of # ).

Upvotes: 3

Related Questions