I have a View that I created programmatically, and I want to have a ripple effect when I select it. I was able to get that working using ?attr/selectableItemBackground. However, I also want to set the background color of the View when I select it. I've tried setBackgroundResource(selectableAttr) and then setBackgroundColor(colorSelectBackground), but the color seems to overwrite the resource, so I only have one or the other. Here's my code:
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
int backRes = typedArray.getResourceId(0, 0);
public void select() {
view.setSelected(true);
view.setBackgroundResource(backRes);
view.setBackground(colorSelectBackground);
}
public void deselect() {
view.setSelected(false);
view.setBackground(colorSelectBackground);
}
Anyone know how I can use both ?attr/selectableItemBackground and also set a background color? Thanks!
EDIT: To clarify, the view in question isn't a button, it's a RelativeLayout.
UPDATE: I never really found a good solution for this. The closest I got was using View.setForeground() to a Drawable from the TypedArray, i.e.
view.setForeground(typedArray.getDrawable(0));
The main drawback to this is that it's only available on API 23+. Let me know if you come up with a better solution.
I'd recommend creating a custom
View, where you can get thepressedColor,defaultColoranddisabledColorfrom the xml.The following code would work for a Material styled button: