An invisible layout behind the fragment is getting clicked: An invisible layout behind the fragment is getting clicked: android android

An invisible layout behind the fragment is getting clicked:


You need to make the root ViewGroup of VenueFragment clickable so it handles click events and they do not pass down (in the z-order sense) to the other Fragment.


Set clickable property on the second fragment's view to true.

For Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:clickable="true" />


Setting an onClickListener on VenueFragment will work, but I'll add a little bit more explanation. Having the main fragment get a touch event is intended behavior to handle the case where your venue fragment is transparent. In that case, it could be reasonable to expect the touch to go through your transparent view to the one underneath. As such, android passes touch events until they can be handled. Setting an onClickListener will handle the event, preventing it from being passed to the next View. If you wanted to be "correct" about preventing clicks on the lower fragment, there are a couple of options:

  1. Remove the onClickListener from the main fragment in OnPause, and set it in OnResume. That way, any time the main fragment is not the active fragment, it cannot be clicked.
  2. Subclass your top level layout in your VenueFragment and override OnTouchEvent to return true. This will essentially do the same thing as setting an onClickListener, but you may find that it makes your intention to block all touch events clearer.