I have a RecyclerView with each item having the "selectableItemBackground" enabled on the item layout to give me that ripple effect on touch. The problem is, the items have an imageView on them and the ripple effect only takes place under the image. How to have the ripple appear on the complete item layout (over the imageView as well), just like when touching on the items on Google Playstore?
Using clickable imagesYou can turn any View , such as an ImageView , into a button by adding the android:onClick attribute in the XML layout. The image for the ImageView must already be stored in the drawable folder of your project.
android:scaleType. Controls how the image should be resized or moved to match the size of this ImageView. android:src. Sets a drawable as the content of this ImageView. android:tint.
Alternatively you can try this:-
Simply, Wrap your ImageView with CardView
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="200dp"
android:clickable="true"
android:focusable="true"
app:cardCornerRadius="4dp"
android:foreground="?android:attr/selectableItemBackground">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
Inserting a View
to other ViewGroup
is affecting app performance, so you can just change your ImageView
to ImageButton
and give it ?attr/selectableItemBackground
as background
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/myButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="?attr/selectableItemBackground"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_arrow_white" />
I solved set in parent content in foreground
propiety value ?android:attr/selectableItemBackground"
and focusable enabled.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true">
<ImageView
android:id="@+id/cover_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/desc_empty"
android:scaleType="centerCrop"
android:src="@drawable/default_image" />
</RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With