SetBackgroundResource acts different from setBackground with RippleDrawable

I am trying to set RippleDrawable as the background of a list RippleDrawable . When using view.setBackgroundResource(R.drawable.ripple) everything works fine. Upon attempt

 view.setBackground(view.getContext().getDrawable(R.drawable.ripple)) 

it works, but the ripple does not appear when the element is touched quickly (I also have an activated / checked state for the element), it appears only when I hold the view pressed.

The setBackgroundResource method in the View class is as follows:

 if (resid != 0 && resid == mBackgroundResource) { return; } Drawable d = null; if (resid != 0) { d = mContext.getDrawable(resid); } setBackground(d); mBackgroundResource = resid; 

so basically what i am trying to do manually.

NOTE. I want to use the setBackground method because I want to programmatically create a RippleDrawable.

Does anyone have an idea why this is happening?

+5
source share
2 answers

Instead of setting ripple in the View row, set it to a ListView using the listSelector attribute:

 <ListView ... android:listSelector="@drawable/ripple" /> 

Programmatically, you can install it using mListView.setSelector(...) .

+1
source

call View.invalidate(); along with View.requestLayout();

The CompoundButton setChecked(boolean) function invalidates the View when used, so the View will be asked to double-check itself and then re-draw or lay out

+1
source

Source: https://habr.com/ru/post/1232418/


All Articles