Referencing a color resource with modified alpha
After searching around a bit to set the color accent as the ripple drawable's color, I've found that it can be done with the aid of a <selector>
.
Add a color
resource folder if not existing and create a new file there, whose base name will be used as color resource. For example, name it my_color_transparent.xml
. Then, paste the following contents.
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/myColor" android:alpha=".5" /></selector>
At this point, you can reference it as @color/my_color_transparent
via XML or programmatically as usual, like colors in the values
folder.
NOTE:The android:alpha
attribute is applied as a mask, so the alpha is multiplied by that of the color specified via the android:color
attribute. As an instance, if @color/myColor
were 20% opaque and android:alpha
were .5
, then the opacity of @color/my_color_transparent
would be 10%.