0

I used CardView layout in list_item.xml "the xml post cell" for RecyclerView", I trying to making the cards prominenced and the color of divider close to gray like this

But it appears as like there is no emergence or color of it,
this image for current situation

this post_item.xml

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    android:elevation="6dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:id="@+id/postTitle"
                style="@style/Base.TextAppearance.AppCompat.Title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="This is the post title,
you can check bla bla bla bla bla lkjdflkjhnfldkjhnflkjhnlfkjnglkjnlkjkln" />

            <TextView
                android:id="@+id/postDescription"
                style="@style/Base.TextAppearance.AppCompat.Body2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:ellipsize="end"
                android:maxLines="4"
                android:text="This is the post titldfkjfdjffkjdhfnkjdsfjnkdsjnksskskjfnksjfnksjfnksjfnkjdfbdnfbjvb jkxzbvkjxcvbvkjve,
you can check bla bla bla bla bla lkjdflkjhnfldkjhnflkjhnlfkjnglkjnlkjkln" />

        </LinearLayout>

        <ImageView
            android:id="@+id/postImage"
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="3"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher" />


    </LinearLayout>

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

I tried to solve the problem by adding this two lines on RecyclerView like this answer but it did not work
android:divider="#E6E6E6" android:dividerHeight="0px"

Dr Mido
  • 2,414
  • 4
  • 32
  • 72

4 Answers4

1

If issue still face issue use Background colour in RecycleView.

Rahul Khatri
  • 1,502
  • 2
  • 13
  • 25
0

After doing some searches, I found this question regarding the same issue

The problem because this option was false in manifest file

<application android:hardwareAccelerated="true" ...>

also this answer contains some choices that were very helpful to me like this

app:cardUseCompatPadding="true"
Dr Mido
  • 2,414
  • 4
  • 32
  • 72
0

Try this code:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="6dp"
    app:cardCornerRadius="3dp"
    >

You must use:

app:cardElevation="6dp"

instead of:

android:elevation="6dp"

To draw shadow you must use hardwareAccelerated drawing

<application android:hardwareAccelerated="true" ...>

Explanation from Android doc :

CardView adds additional padding to draw shadows on platforms before L.

This may cause Cards to have different sizes between L and before L. If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. As an alternative, you can set cardUseCompatPadding flag to true and CardView will add the same padding values on platforms L and after.

Since setting cardUseCompatPadding flag to true adds unnecessary gaps in the UI, default value is false.

Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
0

After setting this options

app:cardUseCompatPadding="true"
app:cardElevation="6dp"

I suggest to add color off white color into RecyclerView

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F5F5F5" // >>> this the color you need
        >

    </androidx.recyclerview.widget.RecyclerView>
Mahmoud Metawee
  • 174
  • 1
  • 4
  • 23