Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setBackgroundResource acts different from setBackground with RippleDrawable

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 ?

like image 684
GeorgeP Avatar asked Oct 30 '22 18:10

GeorgeP


2 Answers

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(...).

like image 186
onosendai Avatar answered Nov 15 '22 04:11

onosendai


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

like image 24
Elltz Avatar answered Nov 15 '22 04:11

Elltz