Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete ripple effect on recycler view item click programmatically

Tags:

android

I need to delete ripple effect on a recycler view item click programmatically. This is because I reuse the adapter in several activities of my app, but there's a particular one where the items shouldn't be clickable, therefore I need to delete the ripple effect, otherwise it looks like it should do something on click, but it is not working.

The only way it works is by removing

android:foreground="?attr/selectableItemBackground"

on the view holder, but as I said, I can't do that because I'm reusing it, and it should show the ripple effect in several other activities. Is there a way to disable it from a particular activity?

like image 847
Praveen P. Avatar asked Mar 01 '26 18:03

Praveen P.


2 Answers

You should let your adapter take in a parameter that determines whether to display the ripple or not.

So start by changing your adapters constructor:

MyAdapter(boolean shouldRipple)

Then you can set the item foreground in onBindViewHolder:

onBindViewHolder {

    itemView.setForeground(...)
}
like image 149
Henry Twist Avatar answered Mar 06 '26 17:03

Henry Twist


You can do one thing. Instead of setting android:foreground by XML, You can set it programatically.

Whenever You do not need this foreground(ripple effect), simply set the null. And when you required this, then You can set this ?attr/selectableItemBackground programatically.

refer this to set it programatically

like image 30
Bhargav Thanki Avatar answered Mar 06 '26 15:03

Bhargav Thanki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!