3

I set CardView and inside of it I put RelativeLayout.

Layout looks something like this:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_basket_list_card_view"
android:orientation="horizontal"
android:layout_width="match_parent"
android:clickable="true"
android:layout_margin="5dp"
card_view:cardCornerRadius="@dimen/basket_list_cardview_corner_radius"
card_view:cardElevation="@dimen/basket_list_cardview_elevation"
card_view:cardUseCompatPadding="true"
android:layout_height="match_parent">

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

...

When I click on CardView on Lollipop phones, I get a nice ripple effect and everyone is happy. But, when I click on that CardView on pre-Lollipop phones, it only gets colored with some blue semi-transparent color. It looks ok with me, but I would like to change that color.

How can I do that?

Best,

Deveti

Draško
  • 2,119
  • 4
  • 41
  • 73
  • I don't think I can answer your question, but where did you look to learn how to use the CardView? Just asking really quick, because I haven't been able to figure it out. – freddiev4 Feb 24 '15 at 22:30
  • https://developer.android.com/training/material/lists-cards.html, http://treyrobinson.net/blog/android-l-tutorials-part-3-recyclerview-and-cardview/ for example, but real start with material design in general was through http://frogermcs.github.io/Instagram-with-Material-Design-concept-is-getting-real/ – Draško Feb 25 '15 at 07:36

1 Answers1

1

You can use

android:background="?attr/selectableItemBackground"

to get a light grey color instead of a blue one. It then uses the App's theme, which is probably the AppCompat theme in your case.

If you want to have full control I'm afraid you have to create your own state list and override selectableItemBackground or create a new item.

Example from the link:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

The drawable can also be a RippleDrawable. You can use the folder "drawable" for the default selection drawable and "drawable-21" for the ripple effect drawable. See this answer for a good overview.

If you use android:foreground it behaves like an overlay, so a transparent black color like #20000000 gives a grey tint.

Community
  • 1
  • 1
CGMan
  • 91
  • 6