Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have selectableItemBackground on Imageview?

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?

like image 538
SergeantPeauts Avatar asked Dec 02 '15 16:12

SergeantPeauts


People also ask

Can ImageView be clickable?

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.

What is SRC in ImageView?

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.


3 Answers

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>
like image 185
Paresh P. Avatar answered Oct 05 '22 08:10

Paresh P.


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" />
like image 36
zihadrizkyef Avatar answered Oct 05 '22 08:10

zihadrizkyef


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>
like image 21
Webserveis Avatar answered Oct 05 '22 07:10

Webserveis