Are there no ripples on the new MaterialCardView?
I used CardView with android:clickable="true" and android:foreground="?android:attr/selectableItemBackground" which worked fine but with the new MaterialCardView there are no ripples at all 😰.
Other Views etc. keep working but I want my card to be clickable (with ripple as feedback).
This is a bug with MaterialCardView. It's overriding the foreground after it's created. I've filled a bug. It should be fixed to show a ripple if the card is clickable without needing to set the foreground.
If you can't wait for a fix you could use CardView from the support lib, or you could change your code to set the foreground drawable after the CardView has been inflated, or we would gladly accept a pull request! Relevant code at: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/card/MaterialCardView.java. It can probably use https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/ripple/RippleUtils.java and a RippleDrawable for SDK >= 21.
In my case, It worked after adding android:clickable="true", android:focusable="true" and app:rippleColor="#cfd8dc" in XML file.
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
app:rippleColor="#cfd8dc">
Ripple effect is working now, but only if you add an OnClickListener on the CardView
Related
By default, when you click on a Button, it will get highlighted. How to do the same thing for TextView when the user click on it?
You should be able to set this in the xml layout file, by setting the view clickable and setting the foreground property correctly.
For example:
...
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
/>
...
Just a note, foreground is only available in Android API version 23 or above. If you need to use it in a version below that, replace android:foreground with android:background. Not exactly the same behaviour (ripple effect is placed in the background of the text, instead of in a layer above of the view as the property name indicates), but it should do the trick.
I have a contraint.Group inside my item_layout that I will use it in a recyclerView:
<android.support.constraint.Group
android:id="#+id/constraintGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="imageView, textView" />
I also set the onClickListener for this group in the activity that works perfectly. The problem is that every time I tap on the group, the ripple effect isn't there.
Anyone knows what should I do to have that effect in a constraint.Group?
I tried adding the attribute android:foreground="?selectableItemBackground" to the parent and the constraint.Group but it didn't work :(
A Group is only for controlling visibility. You cannot control arbitrary UI attributes of the members of a group, such as the background. Instead, you would need to put your background on the actual widgets.
You can try using androidx.constraintLayout.widget.Group. It is an updated version of android.support.constraint.Group.
The following code adds the ripple effect
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
Whenever I am creating a layout, it has a ripple (see image). If I add a checkbox, the ripple just doesn't show. I would like it to show a ripple even though there is a checkbox. How can I do so?
Ripple:
No Ripple (But with checkbox)
Do not add ripple to background use foreground instead.
android:background="#ffffffff"
android:foreground="#ffffffff"
Enable Ripple:
You can achieve this by adding the following to the parent LinearLayout:
android:clickable="true"
android:background="?attr/selectableItemBackground"
Disable Ripple: (As suggested by #Caspain Caldion)
android:background="#ffffffff"
I don't really have much hope for this widget (CardView). It is surely some kind of bug they made and to fix it we need to put some real effort in. If the cardBackgroundColor is not transparent, everything seems to look fine (there is not any border as well as the shadow, although it may be still a little visible but not very sensitive to your eyes).
Now if I set the cardBackgroundColor to transparent (#android:color/transparent) I can see the trace with shadow around the CardView.
I know using the CardView with this style (transparent background with no border and shadow) is a little strange, but the style may change by some settings, so it should be used at first.
Here is the code (mainly for the CardView which causes the undesired effect):
<!-- ra is namespace at http://schemas.android.com/apk/res-auto -->
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ra:cardElevation="0dp"
ra:cardBackgroundColor="#android:color/transparent"
>
Here is the screenshot showing what's wrong with this CardView:
The first image is what I want. The second one is what looking fine if the background is not transparent (white). The last one is the failed image showing some kind of bug as I mentioned.
I wonder if I may have to switch to using some other layout (and if any requirement to support shadow comes, I have to switch to using CardView, which spends my time changing the code, not very conveniently).
I had exactly the same problem, with the elevation showing despite having card_view:cardElevation="0dp" in the CardView layout when the background is transparent. This was when CardView was being used with RecyclerView.
What did solve the problem for me was adding card_view:cardMaxElevation="0dp" to the layout.
Following code works perfectly on Kitkat, but shadows are not visible in Lollipop.
Actually I can see shadow in Android Studio Preview, but not while running on a device/emulator. I'm using CardView for adapter of ViewPager (android.support.v4.view.ViewPager)
<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"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="6dp">
Actually it also shows shadow in L version, but it is based on elevation so you can't see the shadow if card height is match parent
try adding margin to card if you want to see shadow
Found the solution. I'm able to get the shadow effect using following code.
android:background="#android:drawable/dialog_holo_dark_frame"