5

I am trying to only show my FloatingActionButton on one fragment and hide in the rest. I have tried multiple answers and nothing is working. I tried this solution:

Hide a Floating Action Button of another Layout

But it isn't working. I tried adding buttons on two of the fragments, one that shows and one that hides the FAB and that works, but once I remove the button, it won't show automatically. Here is my code:

hidden_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>

HiddenFragment.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();
}

The way I currently have it doesn't work. When I launch the app, the first fragment launched is the HiddenFragment. But when I go to the ShownFragment, the FAB doesn't appear. I tried a different approach, I added a button to each fragment and added the showFloatingActionButton() and hideFloatingActionButton() to the buttons and that works. Does anyone know what I am doing wrong?

Community
  • 1
  • 1
Alexiz Hernandez
  • 349
  • 1
  • 3
  • 13

3 Answers3

12

For showing/hiding a FloatingActionButton with Fragments in a ViewPager, just use a ViewPager.OnPageChangeListener and show the FloatingActionButton for the positions you want, and hide it for the positions you want.

This would go in the 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) {

            }
        });
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • 1
    That worked! It was so obvious, I was thinking about it too much. Thank you so much for your help!! :) – Alexiz Hernandez May 20 '16 at 23:24
  • This example works for only one fragment with fab, but this code show no animation in/out if i want to all fragments to have a fab ... 'cause switch is always showing the fab, never hiding... – Ely Dantas Sep 22 '16 at 21:22
  • @Ely be sure that the fab is in the Activity layout. Just switch the icon/action for each Fragment that uses the fab. See here for more details: http://stackoverflow.com/documentation/android/2979/floatingactionbutton/13346/show-and-hide-floatingactionbutton-on-swipe#t=20160926031742388375 – Daniel Nugent Sep 26 '16 at 03:20
1

Hide the floating button from other fragment. It work in my case.

in MainActivity.java add:

private ActivityMainBinding binding;

public FloatingActionButton getFloatingActionButton() {
     return binding.fab;
}

onCreateView in SecondFragment:

private FloatingActionButton floatingButton;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     floatingButton = ((MainActivity) getActivity()).getFloatingActionButton();
     floatingButton.hide();
     ...
}
Alex IP
  • 21
  • 4
1

It can be adjusted like below, fab visibility or any element of activity during navigation.

navController = findNavController(R.id.nav_host_fragment_activity_main)

    with(binding.editNoteFab) {
        setOnClickListener {
            navController.navigate(R.id.action_listNotesFragment_to_addNoteFragment)
        }
    }

    navController.addOnDestinationChangedListener { _, destination, _ ->
        when (destination.id) {
            R.id.listNotesFragment -> showFloatingButton()
            R.id.editNoteFragment -> hideFloatingButton()
            R.id.addNoteFragment -> hideFloatingButton()
            else -> showFloatingButton()
        }
    }

put that code block in mainActivity.