I've seen some SO questions and they gave some possible methods to achieve what I want. For example:
Use
colorControlHighlight
attribute in styles.xml.Here is my styles-v21.xml:
<style name="SelectableItemBackground"> <item name="android:colorControlHighlight">#5677FC</item> <item name="android:background">?attr/selectableItemBackground</item> </style>
And my widget:
<TextView android:id="@+id/tv_take_photo_as_bt" android:layout_width="280dp" android:layout_height="48dp" android:text="@string/act_take_photo" style="@style/SelectableItemBackground"/>
And it doesn't work. I also tried to add
parent="Theme.AppCompat
to "SelectableItemBackground" style, or change tocolorControlHighlight(no android: prefix)"
, or change to?android:attr/selectableItemBackground
, neither is useful.Use
backgroundTint
attribute in layout.So I add
android:backgroundTint="#5677FC"
to myTextView
. Still useless. Then I tried to changeandroid:backgroundTintMode
tosrc_in
andsrc_atop
, and they never make a difference.
So, how can I change ripple color when I use ?attr/selectableItemBackground
as background. I only focus on Lollipop and above. Thank you in advance!
The accepted answer is wrong.
The correct way to use is what Liuting mentioned in the comment. Use
colorControlHighlight
instead ofandroid:colorControlHighlight
for changing the defaultcolorControlHighlight
fromAppCompat
* Please refer to http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html in the Theming section *
Use the foreground attribute as selectableItemBackground and background attribute as the color you want.
Ripple effect on pre- and Lollipop+ devices
harrane and Liuting are right. The accepted answer is not the best way. Let me show in code how to change ripple color for pre-Lollipop versions and higher
Your AppTheme should inherit from any AppCompat theme and contain colorControlHighlight attribute (without 'android:' prefix)
Your view should contain clickable="true" (or should have a click listener set programmatically) and background should be "?attr/selectableItemBackgroundBorderless" or "?attr/selectableItemBackground" :
Note: that if your parent view has white background you won't see ripple effect since it's white. Change colorControlHighlight value for a different color
Also, if you want different ripple colors on different activities you can set personal theme for each activity in Manifest file, for example:
Different ripple colors for different fragments in the same activity?
You can change attributes of Activity Theme for each fragment in runtime. Just overwrite them before fragment was inflated with your custom style and apply to a current Theme:
in values/styles.xml
Then, in your fragment before inflation in
onCreateView()
:This style will work only for this fragment
Different ripple color for different Views? (Lollipop +)
You can change the ripple color for each view seperately using
colorControlHighlight
attribute, it doesn't work if you apply them to a view directly:you should apply it as a theme:
P.S. Also, sometimes this approach helps if you have unknown issues with ripple and you can't figure it out. In my case, I used 3rd party sliding lib which messed up ripple effects for the entire layout and adding explicitly this theme to all clickable views worked out for me.
Finally I find the solution: instead of using
android:colorControlHighlight
directly in themeSelectableItemBackground
, I should write another style:Then:
Finally add
style="@style/SelectableItemBackground"
toView
in layout.xml.UPDATED ON 2016/8/26 After N's release, I found that sometimes we cannot use this method to set ripple color for some kind of
View
(for example, theCardView
). Now I highly recommend developers usingRippleDrawable
, which can also be declared in xml. Here is an example:I want to show a ripple effect when user touches/clicks a
CardView
above API21, and of course there should be another kind of feedback before Lollipop. So I should write:and
selectable_item_background
indrawable
folder:selectable_item_background
indrawable-v21
folder:finally, the
ripple_black
indrawable
(ordrawable-v21
) folder:That's it. For other views, maybe you should use
android:background="@drawable/selectable_item_background"
. Don't forget to set anOnClickListener
,OnTouchListener
or something like those for them, otherwise ripple won't show.