tags:

views:

50

answers:

1

I've got a LinearLayout with a bunch of buttons on it. This panel is always hidden unless a menu item is chosen.

When the LinearLayout is shown, I want to detect if the user clicks anywhere outside of the panel so that I can hide it again. Is there a way to do this, maybe with detecting focus changes?

I've tried to add an OnFocusChangeListener to the LinearLayout itself (and called setFocusable(true) on it), but the focus change listener never gets called. Furthermore, I'd have to be able to detect if the LinearLayout or any of its children lose focus.

+1  A: 

Try this:

Step #1: Have the LinearLayout watch for touch events, and consume them

Step #2: Have the thing under the LinearLayout watch for touch events -- if it gets one, you know it wasn't from the LinearLayout, so you dismiss the LinearLayout and consume that event, then perhaps unregister for touch events (or just only pay attention to them if the LinearLayout is on-screen)

Step #3: There is no step #3...I think

CommonsWare
Will the LinearLayout still get the touch events if one of the Buttons inside it gets touched?
synic
Touch events propagate outwards, as I understand it, so if the user taps the `Button`, the `Button` will respond. Take all of this with a grain of salt, as I haven't tried this combination of things.
CommonsWare