Set layout_anchor at runtime on FloatingActionButton
A

1

16

I am trying to animate a android.support.design.widget.FloatingActionButton that is pinned to my AppBarLayout. I can set it fine within the layout xml and it shows up fine. However i am doing a Shared Element Transition to this layout and the FAB is showing up before the view is set. I tried to set the visibility to GONE and INVISIBLE but they seem to be disregarded if the layout_anchor is set in the layout xml. Is there anyway around this?

I would like the activity to load with the shared element transition then fade in my FAB. I just can't hide the FAB on load. I could do it without using the layout_anchor but prefer to keep that if possible.

Afterburner answered 23/7, 2015 at 19:36 Comment(0)
P
39

If you have a FAB with the the app:layout_anchor attribute, and you want to set the visibility you should use something like this:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
params.setAnchorId(View.NO_ID);
fab.setLayoutParams(params);
fab.setVisibility(View.GONE);

If you want to set theapp:layout_anchor dinamically you can use the same code:

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(xxxx);
fab.setLayoutParams(p);
Parthen answered 23/7, 2015 at 20:40 Comment(4)
This looks very promising. What about setting the layout_anchorGravity in this way?Afterburner
I can leave the layout_anchorGravity in the layout xml and just set the anchor id like you said above. This is great! ThanksAfterburner
This does work for setting the anchor dynamically, it however does not solve the shared element transition problem mentioned in this question. Setting the anchor is what seems to bork the animation. Without setting any anchor, the animations seem to work fine.Revenuer
This does nothing if I set it after adding the view and crashes if I set it before adding the view...Polemoniaceous

© 2022 - 2024 — McMap. All rights reserved.