4

I'm interested in implementing a FloatingActionButton or FAB across a number of fragments. When the fragment changes, the FAB's icon should change and the button should obviously then do something pertaining to that specific fragment, however for a seamless display, the FAB is located within the Main Activity and not per-fragment.

What's the most proper and/or efficient way of doing this?

I can't wrap my head around it.. I've tried to implement listeners in the Fragment, for the click in the Activity, but it's saying the listeners are Cyclical

Thanks

Mullazman
  • 507
  • 5
  • 15

1 Answers1

7

You can get the Floating Action Button in your fragments e.g in onCreate method of fragment by doing something that

FloatingActionButton fab = (FloatingActionButton)this.getActivity().findViewById(R.id.fab);
MWiesner
  • 8,868
  • 11
  • 36
  • 70
JF YOBOUE
  • 79
  • 1
  • 3
  • Ahh ok, I didn't even think of trying that, of course.. Ok I'll try and implement that also. Currently I managed to get a desired effect by giving individual buttons (per fragment) the same name and linking animations of the buttons using xml like so `android:transitionName="fab_right"` which if I use the coordinator layout view will automatically animate the buttons to relocate per-fragment, giving the illusion of a single button - I suppose this is all that's required. Thanks for your answer, will implement it then mark correct. – Mullazman Jan 06 '16 at 05:38
  • 1
    You don't need to cast views if your "compileSdk" version is 26 or higher. Check my answer. – Chanaka Fernando Jan 05 '18 at 05:58