Zero elevation — zero click handling?

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):

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:

or

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.

--

--

Founder and Senior Android Engineer. Crowdfunding addict. Trying hard to do the right thing and failing more often than not.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sven Bendel

Founder and Senior Android Engineer. Crowdfunding addict. Trying hard to do the right thing and failing more often than not.