Dimezis / BlurView

Dynamic iOS-like blur of underlying Views for Android
Apache License 2.0
3.52k stars 334 forks source link

Is it possible to use BlurView with a Snackbar? #137

Closed elizavetamikhailova closed 3 years ago

elizavetamikhailova commented 3 years ago

Hello! I want to use blurView as a background for my snackbar. I configure my snackbar background like this:

private fun createSnackbar(length: Int) {
    snackBar = Snackbar.make(view, "", length)
    val layout = snackBar.view as Snackbar.SnackbarLayout
    layout.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).visibility =
        View.INVISIBLE
    layout.setBackgroundColor(Color.TRANSPARENT)
    layout.addView(snackView.root, 0)
    snackView.blurView.apply {
        if (isL()) {
            outlineProvider = ViewOutlineProvider.BACKGROUND
            clipToOutline = true
        }
        background = createGradientDrawable(R.color.uikit_neutral_100_50)
    }
    if (isJ1())
        setupBlurBackground()
}

And I configure blurView like this:

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun setupBlurBackground() {
    snackView.root.post {
        snackView.blurView
            .setupWith(snackView.root.parent as ViewGroup)
            .setBlurAlgorithm(RenderScriptBlur(context))
            .setBlurRadius(blurRadius)
            .setBlurAutoUpdate(true)
            .setHasFixedTransformationMatrix(false)
            .setBlurEnabled(true)
    }
}

But it doesn't work, and BlurView doesn't blur anything :(

Снимок экрана 2021-03-15 в 17 57 49

I'm trying to figure out why this doesn't work and can I get it to work?

Dimezis commented 3 years ago

Hey, Could you try replacing .setupWith(snackView.root.parent as ViewGroup) with the root of your activity/fragment?

I guess the problem is that this particular ViewGroup snackView.root.parent might not have any background? You can also debug what snackView.root.parent is referencing exactly to be sure

elizavetamikhailova commented 3 years ago

Yes it worked! Thank you very much!