Search code examples
androidandroid-linearlayoutonclicklistenerontouchlistenerdelegation

Android - Linear Layout default onClickListener that delegates clicks?


I have a Map view with some pins of certain locations, whenever a location is pressed a bottom LinearLayout popup is displayed.

Whenever the user clicks on the map but not on the popup, the popup is dismissed.

We have a bug, that whenever the popup is clicked, it is also dismissed, probably because the event is delegated to the first responder view, which is the map.

However, somehow, by setting . pupup.setOnClickListener(null) fixes the issue and click events are no longer delegated.

I certainly does not understand the logic on this, the popup didn't have any listener neither from XML or Code, and by debugging hasOnClickListeners() resolves to false so I don't get why by setting it again to null removes any delegation.

Any insight on this?


Solution

  • This happens because the base View.setOnClickListener converts the view to clickable, whether you set or remove the OnClickListener

    /**
         * Register a callback to be invoked when this view is clicked. If this view is not
         * clickable, it becomes clickable.
         *
         * @param l The callback that will run
         *
         * @see #setClickable(boolean)
         */
        public void setOnClickListener(@Nullable OnClickListener l) {
            if (!isClickable()) {
                setClickable(true);
            }
            getListenerInfo().mOnClickListener = l;
        }
    

    A better approach to remove unwanted delegation would be to call

    popup.setClickable(true)