ywwynm ywwynm - 6 months ago 869
Android Question

How can I modify ripple color when using ?attr/selectableItemBackground as background?

I've seen some SO questions and they gave some possible methods to achieve what I want. For example:


  1. 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 to
    colorControlHighlight(no android: prefix)"
    , or change to
    ?android:attr/selectableItemBackground
    , neither is useful.

  2. Use
    backgroundTint
    attribute in layout.

    So I add
    android:backgroundTint="#5677FC"
    to my
    TextView
    . Still useless. Then I tried to change
    android:backgroundTintMode
    to
    src_in
    and
    src_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!

Answer

Finally I find the solution: instead of using android:colorControlHighlight directly in theme SelectableItemBackground, I should write another style:

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/ripple_color</item>
</style>

Then:

<style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>

Finally add style="@style/SelectableItemBackground" to View in layout.xml.

Comments