Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23972

Fragment over another fragment issue

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).

Upvotes: 311

Views: 100402

Answers (12)

Yekta Sarıoğlu
Yekta Sarıoğlu

Reputation: 1575

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.

Upvotes: 4

MarsPeople
MarsPeople

Reputation: 1824

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);
}

Upvotes: 4

Miravzal
Miravzal

Reputation: 2035

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>

Upvotes: 29

Hossam Hassan
Hossam Hassan

Reputation: 791

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.

Upvotes: 7

Raju Ugale
Raju Ugale

Reputation: 4291

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;
    }
}

Upvotes: 0

Allan Veloso
Allan Veloso

Reputation: 6389

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.

Upvotes: 1

Eugene Babich
Eugene Babich

Reputation: 1709

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.

Upvotes: 0

JingYeoh
JingYeoh

Reputation: 151

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();

Upvotes: 13

Juan Mendez Escobar
Juan Mendez Escobar

Reputation: 2767

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.

Upvotes: 0

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23972

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;
}

Upvotes: 77

Satish Silveri
Satish Silveri

Reputation: 393

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.

Upvotes: 0

Jure Vizjak
Jure Vizjak

Reputation: 6758

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" />

Upvotes: 647

Related Questions