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).
Visibility
of your main
Fragment
to GONE
when you're not using it.
onClick
method doesn't returns anything. But you give an idea, thanks (I'll post answer soon).
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 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;
}
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>
focuseable
is not really necessary.
focusable="true"
here is just to avoid a warning in Android Studio.
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();
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.
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);
}
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.
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
.
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.
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.
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.
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;
}
}
Success story sharing
RelativeLayout
inside the fragment, and I set the whole view with theclickeable
property. The solution of @Dmitry solve my problem.