Apply Material ripple to ViewPager item

Viewed 724

I would like to add a Material design ripple effect to the Views shown by a ViewPager. My team has been able to do that in other parts of our app by setting ?attr/selectableItemBackground as the background of a View. However, when I assign that as the background of the View returned by my ViewPager's adapter, the effect doesn't seem to work. Has anyone gotten that to work?

From experience, it seems that ViewPagers consume events that prevent certain interactions with their children. For example, ViewPager items don't get onClick() events and you have to work around that in other ways. I wonder if there isn't a similar issue here, where the ViewPager is consuming events that are needed for the ripple to work.

2 Answers

The view pager is intended for swiping, so if you want to click it, you need to specify that, either programmatically, or in the XML file. For instance, add these attributes to the layout of the item of your view pager, not the view pager itself.

android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
Related