I am trying to set a RippleDrawable
as background to a list view row.
When using view.setBackgroundResource(R.drawable.ripple)
everything works fine.
When trying
view.setBackground(view.getContext().getDrawable(R.drawable.ripple))
it works but the ripple doesn't show when quickly touching the item (I also have an activated/checked state for the item), it appears only when I keep the view pressed.
The setBackgroundResource method in View class looks like this :
if (resid != 0 && resid == mBackgroundResource) {
return;
}
Drawable d = null;
if (resid != 0) {
d = mContext.getDrawable(resid);
}
setBackground(d);
mBackgroundResource = resid;
so basically the exact thing I am trying to do manually.
NOTE: I want to use setBackground method because I want to create the RippleDrawable programatically.
Does anybody have an idea why this is happening ?
Instead of setting the ripple on the row View
, set it on the ListView
using the listSelector
attribute:
<ListView
...
android:listSelector="@drawable/ripple" />
Programmatically you can set it with mListView.setSelector(...)
.
call View.invalidate();
along with View.requestLayout();
the CompoundButton
function setChecked(boolean)
when used invalidates the View
hence the View
is asked to re-check itself and then re-drawn or layed out
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With