I'm trying to implement FloatingActionButton from Google Design Support Library into two of three tabs, and according to the Material Design Guidelines - FloatingActionButton it says:
If there is a floating action button on multiple lateral screens (such as on tabs), upon entering each screen, the button should show and hide if the action contained on each is different. If the action is the same, the button should stay on screen (and translate to a new position, if necessary.)
How can I make this kind of transition or animation for the FAB buttons in my app?
That's what worked for me:
Below is the simple way to achieve your desired result
Add two (or equivalent to your tab actions)
FloatingActionButton
in your main activity like belowNow in your MainActivity.java use Fab's default functions to hide and show on each tab selection like below
Call
animateFab
function as belowExpanding upon blackcj's & kirtan403's answers, I have also added the ability to hide the
fab
on a chosen tab (in this case the 1st tab), which answer's bernzkie's question under blackcj's answer.To achieve this I first declared
int[]
's with 3 items, each for thefab
of each of the 3 tabs. I set the first item in each to 0 because that will be the 1st tab's invisiblefab
that doesn't need resources.I then set an
if
statement in theonCreate
method of theActivity
that features thefab
and tabs. This statement hides the fab and scales it down, so that when it is made visible again, it can be made to only scale up, not down unnecessarily, then up again. I set the scale to match final scale of blackcj's scale down animation.Then outside the
onCreate
method, I added blackcj'sanimateFab
method, with kirtan403'srotate
modification. However, I modified theanimateFab
method to also have a conditional statement, where:if it returns to the 1st tab, the
fab
is hidden (it scales down automatically when hiding);when switching from a tab where the fab is already full size and visible to another tab where it is meant to be visible, it performs the full scale down, change & scale up animation;
when switching from the tab with the hidden
fab
(in this case the 1st tab), thefab
is made visible, then scaled up (not scaled down, then scaled up) with the custom animation.Then I just added blackcj's unchanged tab selection listener to the
onCreate
method.Hope this helps, it certainly works flawlessly for me. Thanks to blackcj & kirtan403. :)
Finally, I've found a solution that's pretty easy and shows an animation that's exactly like the attached gif – in @Nauman's oder @Omid's solutions, the show animation starts before the hide animation has finished. But be sure to use the newest Support Library! I've tested it with version 23.2.1.
Use case:
In your xml, place to fabs with unique id's and visibility set to invisible:
Then, add two fields for your fabs to your Activity (you can also use local variables or get the View each time by
findViewById(...)
):In your
onCreate(...)
function, find these views and save them into the declared fields:Next declare a function that shows the right fab for the given position. The default case (tab 3 or more) is quite easy: Just call the
hide()
method on the fabs.show()
andhide()
already implement a scaling animation. But if we hide fab2 on tab 1, we have to wait until it has finished before we can show fab1. So set anFloatingActionButton.OnVisibilityChangedListener
as parameter for thehide(...)
method and show the desired new fab in theonHidden(...)
method of that listener. The result is this:That was the most difficult part! Now add a listener to the ViewPager to call the
showRightFab(...)
function each time the selected tab changes.At last, call the function one time manually in the
onCreate(...)
method to show the fab at the default tab, because theViewPager.OnPageChangeListener
'sonPageSelected(...)
method isn't called on startup (e. g. otherwise if you open the app and it shows tab 1, no fab will be shown because theshowRightFab(...)
function has never been called).That work's perfectly in my application!
you can add a listener to viewpager and show and hide fab according to its state when you start scrolling the viewpager this is the order of states SCROLL_STATE_DRAGGING SCROLL_STATE_SETTLING SCROLL_STATE_IDLE
for example:
Extending blackcj's Answer, The solution worked really good as explained. However I would like to add something in that.
I have watched that video in slow motion. The drawable and fab are animating differently. When hiding, fab and drawable are in sync. While showing, fab is coming back first, and after 60-70 percent completion the drawable start animation from 0 and rotating and scaling coming to full size.
However, I was not able to achieve drawable animating saperatly. But, I managed to rotate and scale with different Interpolators and slightly modified time. So it seems more like in the video which is also presented in google design guidelines.
And tab tab change listener as usual: