0

I want to add a drawable in my recyclerview to cover whole the card view but it is set to each card separately I want one single background PS: See image for further clarification

I have add background in cardview and recyclerview but nothing changed

here is my recyclerview

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/harampak"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Suplications/Dua"
    android:textAlignment="center"
    android:textColor="@color/green"
    android:textSize="25sp"
    android:textStyle="bold" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginEnd="4dp"
    android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>

card item

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">


<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="4dp"
    app:cardElevation="1dp"
    app:cardMaxElevation="1dp"
    app:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:padding="4dp"
            android:text="After Rainfall"
            android:textColor="@color/green"
            android:textSize="25sp" />

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

shown on androidstudio enter image description here

on device enter image description here

Tanveer Ahmed
  • 426
  • 1
  • 4
  • 15

1 Answers1

0

If you are trying to set different background images in each RecyclerView item. Then you need to two thing in RecyclerVeiw Adapter.

  1. Images array And
  2. set these Images as background of CardView in onBindViewHolder method.

like this :

      public int[] images = { R.drawable.image, R.drawable.image2, R.drawable.image3, R.drawable.image4 };

        @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            holder.cardView.setBackgroundResource(images[position % 4]);
    }

    public class LectureViewHolder extends RecyclerView.ViewHolder {
           public CardView cardView;
           cardView = itemView.findViewById(R.id.card_view);
    }

and they will be repeate after complete Images Array.