How to select selectableItemBackground in Imageview?

I have a RecyclerView with each element on which the "selectableItemBackground" parameter is enabled on the elements layout to give me a ripple effect when touched. The problem is that the elements have an ImageView on them, and the ripple effect only occurs below the image. How to make ripples appear in the full layout of the element (above the sample too), just like when you touch elements in the Google Playstore?

+7
source share
4 answers

Alternatively you can try this: -
Just 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> 
+11
source

I decided to set the value of ?android:attr/selectableItemBackground" in the parent content in foreground and activate focus.

 <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> 
+2
source

Inserting a View into another ViewGroup affects the performance of the application, so you can just change the ImageView to ImageButton and set ?attr/selectableItemBackground as the 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" /> 
+1
source

Try using android:drawSelectorOnTop="true" in your RecyclerView in an XML file. And set the Ripple effect selector as the android:background property of your RecyclerView cell .

An example of a selector for a cell in the drawable-v21 folder:

ripple_selector.xml

 <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/preference_item_pressed"> <!-- ripple color --> <item android:id="@android:id/mask"> <color android:color="@android:color/white" /> </item> <!-- normal color --> </ripple> 

And an example of a cell with selector background:

cell.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="72dp" android:layout_height="72dp" android:background="@drawable/ripple_selector"> </RelativeLayout> 
0
source

Source: https://habr.com/ru/post/1237267/


All Articles