How to set Ripple effect on a LinearLayout programmatically?
Asked Answered
P

2

46

I want to set the background android.R.attr.selectableItemBackground to a LinearLayout. When using XML there are no problems (it works)

<LinearLayout
    android:id="@+id/llMiner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true" >

... but I have to do this in java code, so I've tried this

llMiner.setClickable(true);
llMiner.setBackgroundResource(android.R.attr.selectableItemBackground);

... and it doesn't work, infact I get a NotFoundException on this second line. So after I've tried this variant thinking that the resource is a Color.

llMiner.setClickable(true);
llMiner.setBackgroundColor(android.R.attr.selectableItemBackground);

This one doesn't launch exception, but... doesn't work (there are no changing of background when pressing, but the state change in pressed as it have to do)... any suggestion?

Parisparish answered 4/1, 2012 at 19:18 Comment(6)
are you changing the color in onClick method of your activity..???Tobietobin
Then i think you must override onClick method to get your functionality done.Tobietobin
I am trying to do the same thing and no success so far. If you found a solution please let me know, or if someone knows if this can actually be done, give feedback. Thx!Osiris
For anyone looking for the support lib's counterpart, it is android.support.design.R.attr.selectableItemBackground.Idealistic
My previous comment doesn't seem to work now (or maybe it never did). What works now is R.attr.selectableItemBackground.Idealistic
@Parisparish Please choose my answer if you don't mind :)Deluxe
D
113

You can use this way.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // If we're running on Honeycomb or newer, then we can use the Theme's
    // selectableItemBackground to ensure that the View has a pressed state
    TypedValue outValue = new TypedValue();
    this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
}
Deluxe answered 22/1, 2015 at 11:17 Comment(4)
Not working for me, white color, they don't arrive to keep the colorAccent from theme on pre lollipop deviceDreyfus
If you want to keep your theme colors use R.attr.selectableItemBackground instead of android.R.attr.selectableItemBackgroundBoletus
Using R.attr.selectableItemBackground will use the AppCompat's attribute (if you are including that) and falls back to fade in/out instead of ripple for pre Lollipop, by default. Note that R is your packagename.R.Idealistic
Upvote for the working answer, it really helps, ThanksNowlin
F
1

Use this helpful extenstion

fun Context.makeCircleRippleDrawable(
    @ColorInt rippleColor: Int = ContextCompat.getColor(this, R.color.black_alpha_25),
    @ColorInt backgroundColor: Int = ContextCompat.getColor(this, android.R.color.transparent),
    @ColorInt disabledColor: Int = backgroundColor,
    elevation: Float = 0F
): Drawable {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        val content: GradientDrawable?
        val mask: GradientDrawable?

        if (backgroundColor == Color.TRANSPARENT) {
            content = null
            mask = GradientDrawable()
            mask.setColor(rippleColor)
            mask.shape = GradientDrawable.OVAL
        } else {
            content = GradientDrawable().also {
                it.shape = GradientDrawable.OVAL
                it.color = ColorStateList(
                    arrayOf(
                        intArrayOf(android.R.attr.state_activated),
                        intArrayOf(android.R.attr.state_enabled),
                        intArrayOf(-android.R.attr.state_enabled)
                    ),
                    intArrayOf(
                        backgroundColor,
                        backgroundColor,
                        disabledColor
                    )
                )
            }
            mask = null
        }

        RippleDrawable(
            ColorStateList(
                arrayOf(
                    intArrayOf(android.R.attr.state_pressed),
                    intArrayOf(android.R.attr.state_focused),
                    intArrayOf(android.R.attr.state_activated)
                ),
                intArrayOf(
                    rippleColor,
                    rippleColor,
                    rippleColor
                )
            ),
            content,
            mask
        )
    } else {

        val shapePressed = GradientDrawable()
        shapePressed.shape = GradientDrawable.OVAL
        shapePressed.setColor(rippleColor)

        val shapeDefault = GradientDrawable().also {
            it.shape = GradientDrawable.OVAL
            it.color = ColorStateList(
                arrayOf(
                    intArrayOf(android.R.attr.state_activated),
                    intArrayOf(android.R.attr.state_enabled),
                    intArrayOf(-android.R.attr.state_enabled)
                ),
                intArrayOf(
                    backgroundColor,
                    backgroundColor,
                    disabledColor
                )
            )
        }

        val stateListDrawable = StateListDrawable()
        stateListDrawable.addState(
            intArrayOf(
                android.R.attr.state_pressed,
                android.R.attr.state_enabled
            ), shapePressed
        )
        stateListDrawable.addState(intArrayOf(), shapeDefault)
        stateListDrawable
    }
}
Fennell answered 17/1, 2022 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.