24

Maybe this question has been asked before, but I couldn't find a precise answer. I have implemented the RecyclerView widget as a list in my L test application and I want to get the 'material effect' when you click on an item form the list. I implemented an onClickListener in my ViewHolder creation and set the attribute

android:background="?android:attr/selectableItemBackground"

as a background to my list item layout (to the parent).

But none of this worked. When I click on the items NOTHING happens'. There is no effect, holo, material, none... Please point out if I am doing something wrong here... Thx

Sandra
  • 4,239
  • 10
  • 47
  • 80

3 Answers3

41

Sandra's answer didn't work for me. I needed one more property in my list item layout:

android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"

Note: if you get an error when pasting in the last line, then your android app is set on version 10 or below. Just right click on the error in Android Studio and set it so it will create a v11 version of your layout as well. Then, in the original layout, make sure to delete

android:background="?android:attr/selectableItemBackground"`

This is because the animation isn't supported in v10 versions of android or lower. With these two layout files set up, the animation will correctly show in v11+ versions of Android and of course it won't be shown in lower versions.

(or just increase the minSdkVersion version of your app to higher than 10)

Micro
  • 10,303
  • 14
  • 82
  • 120
  • 2
    I didn't put the android:background="?android:attr/selectableItemBackground"` in the answer of my question, because I explained in my question itself, that I have it already implemented in code.. sorry for the confusion – Sandra Dec 02 '15 at 09:14
  • 2
    setFocusable(true) is unnecessary here and actually excessive as long as item could get into the sate of 'focused item' and got gray background. The ripple effect will work even without this attribute set – Mando Apr 19 '16 at 21:25
37

I made a silly mistake and did not put

android:clickable="true"
android:focusable="true"

to my list item layout. I think this was not required pre L, but it doesn't matter because that was the problem in this case.

Sandra
  • 4,239
  • 10
  • 47
  • 80
  • Do you know how to add a bottom border to the item ? All methods I found are using the background of the root layout, but I can't use it as it is already set for the onclick effect. – Link14 Nov 25 '14 at 02:51
  • 1
    @Link14 you can implement item decoration (you can call addItemDecoration() to the recyclerView object). To implement the wanted decoration (border, or sth else) you need to add class that extends RecyclerView.ItemDecoration to your project and modify it to your needs. You will use this class as an argument in addItemDecoration(). There is much more explanation about this in this thread http://stackoverflow.com/questions/24618829/how-to-add-dividers-and-spaces-between-items-in-recyclerview. RecyclerView.ItemDecoration class available here https://gist.github.com/alexfu/0f464fc3742f134ccd1e – Sandra Dec 18 '14 at 08:55
  • setFocusable(true) is unnecessary here and actually excessive as long as item could get into the sate of 'focused item' and got gray background. The ripple effect will work even without this attribute set – Mando Apr 19 '16 at 21:25
4
android:clickable="true"
android:focusable="true"

these lines are no longer used. just adding

android:background="?android:attr/selectableItemBackground"

working for click effect.

Cüneyt
  • 2,565
  • 25
  • 31