ZieIony / Carbon

Material Design implementation for Android 4.0+. Shadows, ripples, vectors, fonts, animations, widgets, rounded corners and more.

Home Page:https://androidreclib.wordpress.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to set RippleDrawable programmatically

ali-star opened this issue · comments

i'm trying to set RippleDrawable programmatically but no result after hours... any help?

Do you mean the Carbon's RippleDrawable? If so, then the following code is used by Carbon to init RippleDrawable using xml params.

public static void initRippleDrawable(RippleView rippleView, TypedArray a, int[] ids) {
    int carbon_rippleColor = ids[0];
    int carbon_rippleStyle = ids[1];
    int carbon_rippleHotspot = ids[2];
    int carbon_rippleRadius = ids[3];

    View view = (View) rippleView;
    if (view.isInEditMode())
        return;

    ColorStateList color = getDefaultColorStateList(view, a, carbon_rippleColor);
    if (color == null)
        color = a.getColorStateList(carbon_rippleColor);

    if (color != null) {
        RippleDrawable.Style style = RippleDrawable.Style.values()[a.getInt(carbon_rippleStyle, RippleDrawable.Style.Background.ordinal())];
        boolean useHotspot = a.getBoolean(carbon_rippleHotspot, true);
        int radius = (int) a.getDimension(carbon_rippleRadius, -1);

        rippleView.setRippleDrawable(RippleDrawable.create(color, style, view, useHotspot, radius));
    }
}

Setting Carbon's RippleDrawable should be as easy as calling the last line. Please note that this call composes RippleDrawable with the current background.

rippleView.setRippleDrawable(RippleDrawable.create(color, style, view, useHotspot, radius));

You can also set RippleDrawable as background.

view.setBackground(RippleDrawable.create(color, style, view, useHotspot, radius));

Ok but how i can change the RippleDrawable color? i'm stuck in this part...

You can pass any color as the first parameter, just like it's done in the example I posted.