Java – How do I hide and show FloatingActionButtons using Fragments in ViewPager?

How do I hide and show FloatingActionButtons using Fragments in ViewPager?… here is a solution to the problem.

How do I hide and show FloatingActionButtons using Fragments in ViewPager?

I’m trying to show my FloatingActionButton on just one fragment and hide it on the rest. I tried multiple answers but nothing worked. I tried this solution :

Hide a Floating Action Button of another Layout

But it doesn’t work. I tried adding buttons on two fragments, one showing FAB and one hiding FAB and it works, but once I remove the button, it doesn’t show automatically. Here is my code :

Hide fragment .xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>

shown_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>

Hide fragment .java:

public class HiddenFragment extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View view = inflater.inflate(R.layout.hidden_fragment, container, false);

final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton();

if (fab != null) {
            ((MainActivity) getActivity()).hideFloatingActionButton();
        }

return view;
    }

}

ShownFragment.java:

public class ShownFragment extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View view = inflater.inflate(R.layout.hidden_fragment, container, false);

final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton();

if (fab != null) {
            ((MainActivity) getActivity()).showFloatingActionButton();
        }

return view;
    }

}

In my MainActivity .java I have this:

public FloatingActionButton getFloatingActionButton() {
    return fab;
}

public void showFloatingActionButton() {
    fab.show();
}

public void hideFloatingActionButton() {
    fab.hide();
}

My current approach doesn’t work. When I launch the application, the first fragment launched is HiddenFragment. But when I go to ShownFragment, the FAB doesn’t appear. I tried a different approach, I added a button to each fragment and added showFloatingActionButton() and hideFloatingActionButton() to the button, and it worked. Does anyone know what I’m doing wrong?

Solution

To show/hide a FloatingActionButton

with fragments in ViewPager, simply use ViewPager.OnPageChangeListener and display the FloatingActionButton where you want it, and hide it where you want.

This will take you to Activity :

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
            public void onPageSelected(int position) {
                switch (position) {
                case 0:
                    fab.hide();
                    break;
                case 1:
                    fab.show();
                    break;
                case 3:
                    fab.hide();
                    break;
                default:
                    fab.hide();
                    break;
                }
            }

@Override
            public void onPageScrollStateChanged(int state) {

}
        });

Related Problems and Solutions