-3

I am using a RecyclerView with its item as a CardView. I have the card view's background as android:background="?android:attr/selectableItemBackground".

I tried setting the foreground, tried giving a LinearLayout as a parent and set its background etc, but nothing works.

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp">

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

        <!--TextView-->
        <!--Imageview-->
        <!--Textview-->
   </RelativeLayout>
</android.support.v7.widget.CardView>

Why is this behavior?

Edit: The CardView has an onClickListener which works.

Edit 2: I added a RelativeLayout as a parent to the CardView. And made that clickable and gave it a selectable item background. Still no ripples.

 <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="?android:attr/selectableItemBackground"
      android:clickable="true"
      android:focusable="true">

      <android.support.v7.widget.CardView
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          card_view:cardCornerRadius="2dp"
          card_view:cardElevation="2dp">

          <!-- ... -->

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

Edit 3: I posting my exact layout code structure. I had failed to mention the data binding part. But I hope that is not blocking the ripples.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
         <import type="android.view.View"/>
         <variable
             name="viewModel"
             type="com.xxx.xxx.viewmodel.genericviewmodel" />
    </data>


    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="2dp">

        <RelativeLayout
            android:id="@+id/c_open_case_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp">

            <!--TextView-->
            <!--Imageview-->
            <!--Textview-->
            <!--TextView-->
            <!--Imageview-->
            <!--Textview-->
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</layout>
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Ashwin
  • 12,691
  • 31
  • 118
  • 190

1 Answers1

0

Update: After all trials to reproduce your problem, I think that there is no problem with CardViews or data binding. If you're not satisfied with the answer, you can always submit a bug report to the Android dev team.


Use android:foreground and make sure that the CardView is clickable:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp">

    <!-- ... -->

</android.support.v7.widget.CardView>
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
  • @Vujasin manojilovic : doesn't work – Ashwin Feb 26 '16 at 05:04
  • @Ashwin Which version of Android are you using? I use this method in my app and it works. Did you try implementation of a custom `RippleDrawable`? – Vukašin Manojlović Feb 26 '16 at 09:46
  • Manojlovic : Marshmallow – Ashwin Feb 26 '16 at 10:09
  • @Ashwin I'm on Marshmallow too and can't reproduce your problem. I'm using the layout which is in the question (`CardView` > `RelativeLayout` > `TextView`s and `ImageView`). See [this screenshot](http://imgur.com/O98DX0p). The problem is somewhere else. Ping me if you want to send you the sample code. – Vukašin Manojlović Feb 26 '16 at 19:04
  • Manojlovic : yes, I would like to see your code. – Ashwin Feb 27 '16 at 06:32
  • @Ashwin I've used [Android RecyclerView Sample](https://github.com/googlesamples/android-RecyclerView) with modified [`text_row_item.xml`](https://gist.github.com/anonymous/df043e0235cc9ac9bd6f). – Vukašin Manojlović Feb 27 '16 at 11:58
  • Manoklovic : Hey, my layout is similar to that of yours. I have added some extra info in EDIT 3. Check it out. – Ashwin Feb 28 '16 at 15:35
  • @Ashwin I've implemented data binding and ripples were shown on `CardView` click. Without the original code, I can't reproduce the problem. – Vukašin Manojlović Feb 29 '16 at 15:24