Zero elevation — zero click handling?

Sven Bendel
2 min readJun 16, 2019

--

Photo by Jeff Fielitz on Unsplash

Something I’ve only discovered after 10 years of Android development: Setting zero elevation on clickable elements causes them to not being clickable anymore!

I haven’t yet investigated whether this is a bug or a feature, but here is a simple workaround (by means of BindingAdapters, but of course you can also use the supplied View-Extension function on its own):

@BindingAdapter("elevation")
fun setAppElevation(view: View, elevation: Float) {
setElevation(view, elevation)
}

@BindingAdapter("android:elevation")
fun setAndroidElevation(view: View, elevation: Float) {
setElevation(view, elevation)
}

private fun setElevation(view: View, elevation: Float) {
if (elevation == 0f) {
view.setZeroElevation()
} else {
view.elevation = elevation
}
}

fun View.setZeroElevation() {
elevation = 0f
bringToFront() // this fixes 0 elevation views not being clickable
}

The trick is to call bringToFront() after setting the elevation to 0dp - this makes the View recognize click events again. By using BindingAdapters and forwarding to View.elevation we ensure that we don't break existing elevation functionality for Views.

You can simply use this in your layout XML like this:

<View
...
app:elevation="@{0}" />

or

<View
...
android:elevation="@{@dimen/zero}" />

where @dimen/zero is just a fictional 0dp dimension resource which is automatically converted to float pixels by data binding, but obviously it could be any expression resulting in a float.

--

--

Sven Bendel
Sven Bendel

Written by Sven Bendel

Senior Android Engineer @Photoroom. Founder. Crowdfunding addict. Trying hard to do the right thing and failing more often than not. https://proven.lol/286e9b

No responses yet