Search code examples
androidstatusbarnavigationbar

No navigation/status bar touch listener exists, why?


My goal is: after a user touches navigation bar (on some tablets navigation/status bar) on the bottom of the screen, I catch this event and run a method that dims this bar after a few seconds.

But as I was doing my internet research it appears that this is not possible. I tried to work around it, but every touch event works only in the pixel boundaries above the bar.

So my question is: what is the reason that it is not possible to catch navigation/status bar touch events?


Solution

  • First of all, the navigation bar and status bar aren't a part of your application window... they are system windows owned by the OS and run by an entirely separate process. There's no way to attach a touch listener to a View that live in another window without explicit permission from the OS, as that would make it possible for third party apps to intercept touch events dispatched to other applications. This would obviously be a major security vulnerability if it were possible.

    Efficiency is another reason why the Android team probably didn't expose the ability to listen for touch events in the navigation/status bars. Like I said before, the navigation/status bars are windows in a separate process, so even if they had a good reason create some sort of touch event listener for applications, dispatching touch event coordinates to your application would involve a ton of inter-process communication. Implementing such a feature would therefore probably require some major design decisions on Google's end.

    That said, there are of course some callbacks that you can use to listen for certain events. For example, you can override onBackPressed() or onKeyUp()/KEYCODE_HOME to listen for back/home button clicks respectively. Other than that, unless the Android Framework exposes API methods that allow you to achieve what you are looking for, you might not have much luck.