Adding Ripple Effect to RecyclerView item
I figured out. The only thing that I had to do is to add this attribute:
android:background="?android:attr/selectableItemBackground"
to the root element of the layout that my RecyclerView adapter inflates like that:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="?android:attr/selectableItemBackground"
tools:background="@drawable/bg_gradient">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:id="@+id/shoppingListItem"
android:hint="@string/enter_item_hint"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shopping_list_item_checkbox_label"
android:id="@+id/shoppingListCheckBox"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:checked="false"/>
</RelativeLayout>
Result:
If you are still not able to see ripple effect, add these lines also to the root element of the layout.
android:clickable="true"
android:focusable="true"
Recyclerview item click ripple effect
Adding the android:background="?attr/selectableItemBackground"
to the top most parent of your item layout should do the trick.
However in some cases it still misses the animation, adding the android:clickable="true"
does it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/txtTitle"
style="@style/kaho_panel_sub_heading_textview_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtDate"
style="@style/kaho_content_small_textview_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
RecyclerView ripple effect not working
Looking to the LinearLayout parent view background, I saw that the background attribute is not correct (you forgot the android between the question mark and the :attr).
android:background="?android:attr/selectableItemBackground"
Setting Ripple effect to RecyclerView CardView item without having to double tap for an item action
I solved this by totally removing android:focusableInTouchMode = "true"
& android:foreground
from the CardView
, and adding the ripple effect to the root item within the CardView with android:background="?android:attr/selectableItemBackground"
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_margin="@dimen/margin_8dp"
app:cardBackgroundColor="@color/cyan"
app:cardCornerRadius="20dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="@dimen/margin_8dp">
<!-- Underlying views-->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Delete ripple effect on recycler view item click programmatically
You should let your adapter take in a parameter that determines whether to display the ripple or not.
So start by changing your adapters constructor:
MyAdapter(boolean shouldRipple)
Then you can set the item foreground in onBindViewHolder
:
onBindViewHolder {
itemView.setForeground(...)
}
Ripple effect over a RecyclerView item containing ImageView
add below code to your parent layout
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
if you want custom ripple effect add this ripple_custom.xml in your drawable-v21
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorHighlight">
<item
android:id="@android:id/mask"
android:drawable="@color/windowBackground" />
</ripple>
to support older version add ripple_custom.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/colorHighlight" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>
Related Topics
How to Set Android_Home Path in Ubuntu
Eclipse Reports Rendering Library More Recent Than Adt Plug-In
How to Start an Intent by Passing Some Parameters to It
Closing the Database in a Contentprovider
"Failure Delivering Result " - Onactivityforresult
Android Webview with Garbled Utf-8 Characters
Android How to Turn on Do Not Disturb (Dnd) Programmatically
Android, How to Not Destroy the Activity When I Rotate the Device
Apache Httpclient on Android Producing Certpathvalidatorexception (Issuername != Subjectname)
Update Android Sdk Tool to 22.0.4(Latest Version) from 22.0.1
Android Vpnservice to Capture Packets Won't Capture Packets
Android Character by Character Display Text Animation
Differencebetween Background, Backgroundtint, Backgroundtintmode Attributes in Android Layout Xml
How to Detect If User First Time in Firebase
How to Display Input Errors in Popup
Importing Google-Play-Services Lib into Intellij Idea 12 (And 13)