Fragment over another fragment issue

AndroidAndroid Fragments

Android Problem Overview


When I'm showing one fragment (which is full screen with #77000000 background) over another fragment (let's call it main), my main fragment still reacts to clicks (we can click a button even if we don't see it).

Question: how to prevent clicks on first (main) fragment?

EDIT

Unfortunately, I can't just hide main fragment, because I'm using transparent background on second fragment (so, user can see what located behind).

Android Solutions


Solution 1 - Android

Set clickable property on the second fragment's view to true. The view will catch the event so that it will not be passed to the main fragment. So if the second fragment's view is a layout, this would be the code:

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

Solution 2 - Android

Solution is pretty simple. In our second fragment (that overlaps our main fragment) we just need to catch onTouch event:

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstance){
	View root = somehowCreateView();

	/*here is an implementation*/

	root.setOnTouchListener(new View.OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			return true;
		}
	});
	return root;
}

Solution 3 - Android

Just add clickable="true" and focusable="true" to parent layout

 <android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="true"
      android:focusable="true">
    
      <!--Your views-->
    
 </android.support.constraint.ConstraintLayout>

If you are using AndroidX, try this

 <androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="true"
      android:focusable="true">
        
          <!--Your views-->
        
 </androidx.constraintlayout.widget.ConstraintLayout>

Solution 4 - Android

You should hide the first fragment when you are showing the second Fragment if two fragments is placed in same container view.
>If you want to know more questions about how to solve problems about Fragment, you can see my library: https://github.com/JustKiddingBaby/FragmentRigger

FirstFragment firstfragment;
SecondFragment secondFragment;
FragmentManager fm;
FragmentTransaction ft=fm.beginTransaction();
ft.hide(firstfragment);
ft.show(secondFragment);
ft.commit();

Solution 5 - Android

You need to add android:focusable="true" with android:clickable="true"

Clickable means that it can be clicked by a pointer device or be tapped by a touch device.

Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.

Solution 6 - Android

Metod 1:

You can add to all fragments layout

android:clickable="true"
android:focusable="true"
android:background="@color/windowBackground"

Metod 2: (Programmatically)

Extend all fragment from FragmentBase etc. Then add this code to FragmentBase

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getView().setBackgroundColor(getResources().getColor(R.color.windowBackground));
    getView().setClickable(true);
    getView().setFocusable(true);
}

Solution 7 - Android

There is more than one solution that some of us contributed to this thread but also I would like to mention one other solution. If you don't fancy putting clickable and focusable equals true to every layout's root ViewGroup in XML like me. You can also put it to your base if you have one just like below;

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) : View? {
        super.onCreateView(inflater, container, savedInstanceState)

        val rootView = inflater.inflate(layout, container, false).apply {
            isClickable = true
            isFocusable = true
        }

        return rootView
    }

> You can also use inline variable but I did not prefer it for personal reasons.

I hope it helps for the ones who hate layout XMLs.

Solution 8 - Android

The acceptable answer will "work", but will also cause performance cost (overdraw, re-measuring on orientation change) as the fragment on the bottom is still being drawn. Maybe you should simply find the fragment by tag or ID and set visibility to GONE or VISIBLE when you need to show again.

In Kotlin:

fragmentManager.findFragmentByTag(BottomFragment.TAG).view.visibility = GONE

This solution is preferable to the alternative hide() and show() methods of FragmentTransaction when you use animations. You just call it from the onTransitionStart() and onTransitionEnd() of Transition.TransitionListener.

Solution 9 - Android

What u can do is u can give a Blank click to the previous fragment's layout by using onClick property to parent layout of that main fragment and in activity you can create a function doNothing(View view) and do not write anything in it. This will do it for you.

Solution 10 - Android

This sounds like a case for DialogFragment. Otherwise with Fragment Manager commit one to hide and the other one to show. That has worked for me.

Solution 11 - Android

The adding of android:clickable="true" didn't work for me. This solution not works on CoordinatorLayout when it's a parent layout. That's why I've made RelativeLayout as parent layout, added android:clickable="true" to it and placed CoordinatorLayout on this RelativeLayout.

Solution 12 - Android

I had multiple fragments with same xml.
After spending hours, I removed setPageTransformer and it started working

   //  viewpager.setPageTransformer(false, new BackgPageTransformer())

I had scalling logic.

public class BackgPageTransformer extends BaseTransformer {

	private static final float MIN_SCALE = 0.75f;

	@Override
	protected void onTransform(View view, float position) {
		//view.setScaleX Y
	}

	@Override
	protected boolean isPagingEnabled() {
		return true;
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDmitry ZaytsevView Question on Stackoverflow
Solution 1 - AndroidJure VizjakView Answer on Stackoverflow
Solution 2 - AndroidDmitry ZaytsevView Answer on Stackoverflow
Solution 3 - AndroidMiravzalView Answer on Stackoverflow
Solution 4 - AndroidJingYeohView Answer on Stackoverflow
Solution 5 - AndroidHossam HassanView Answer on Stackoverflow
Solution 6 - AndroidMarsPeopleView Answer on Stackoverflow
Solution 7 - AndroidYekta SarıoğluView Answer on Stackoverflow
Solution 8 - AndroidAllan VelosoView Answer on Stackoverflow
Solution 9 - AndroidSatish SilveriView Answer on Stackoverflow
Solution 10 - AndroidJuan MendezView Answer on Stackoverflow
Solution 11 - AndroidZhebzhik BabichView Answer on Stackoverflow
Solution 12 - AndroidAskQView Answer on Stackoverflow