Change the viewing color, but keep the ripple and touch state

From the box you can get the state of white ripple / touch:? Attr / selectableItemBackground I would like to be able to do any color and ripple, and the touch state only works based on the color that I set. It would be nice if I could do it via android: theme

I need this to work with API 15+

I know that I can do a selector and ripples, but then I have to do a color selector, since you cannot do attr in xml pre API 21.

If I set only the background color, then the ripple and touch state do not work.

I just want it to be as simple and reusable as possible.

Is there something that I don't see, or some solution that is relatively clean.

+4
source share
2 answers

What you already posted is very true:

  • Unable to create ripple selector by specifying only color
  • cannot use theme attributes in xml using API <21

I don’t think there is anything more than just using a library or coding. From the code, you can generate ripples, use theme attributes, and more.

I had the same problem and I just wrote a bunch of classes to easily create ripples. For example, to get the colors of themes, I wrote a simple ColorStateList class:

public class ControlCheckedColorStateList extends ColorStateList {
    public ControlCheckedColorStateList(Context context) {
        super(new int[][]{
                new int[]{android.R.attr.state_checked},
                new int[]{}
        }, new int[]{
                getThemeColor(context, R.attr.colorPrimary),
                getThemeColor(context, R.attr.colorControl)
        });
    }

    public static int getThemeColor(Context context, int attr) {
        Resources.Theme theme = context.getTheme();
        TypedValue typedvalueattr = new TypedValue();
        theme.resolveAttribute(attr, typedvalueattr, true);
        return typedvalueattr.resourceId != 0 ? context.getResources().getColor(typedvalueattr.resourceId) : typedvalueattr.data;
    }
}

Then I added attributes for color and ripple style. I also had to override the setBackground methods, so setting ripples to represent does not clear my background. Now setting a ripple with a custom color is as simple as:

<carbon.widget.Button
    android:background="#ffffffff"
    app:carbon_rippleColor="#40ff0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

, . , , , . github. - , .

+1

, -, colorControlHighlight, . <item name="android:colorControlHighlight">@color/rippleColor</item> .

API 15+, , AppCompat - , . FAQ , " " :

  • ?attr/selectableItemBackgroundBorderless - pre-Lollipop
  • ?attr/selectableItemBackground - Lollipop

( )

,

+2

All Articles