Floating Action Button expansion

Knossos picture Knossos · Apr 16, 2015 · Viewed 11.8k times · Source

According to the new Android Design Guidelines for the Floating Action Button, it should be reasonable to transform the Floating Action Button into a Toolbar.

Are there any samples / examples to perform such a transformation?

Answer

Atlas91 picture Atlas91 · Apr 30, 2015

To use reveal animation you need add a onLayoutChange listener to the view in onCreateView callback like this:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_map_list, container, false);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    v.removeOnLayoutChangeListener(this);
                    revealView(view);
                }
            });
        }
        return view;
    }

where the revealView() method will be:

private void revealView(View view) {

    toolbar = view.findViewById(R.id.mytoolbar);

    int cx = (view.getLeft() + view.getRight()) / 2;
    int cy = (view.getTop() + view.getBottom()) / 2;
    float radius = Math.max(infoContainer.getWidth(), infoContainer.getHeight()) * 2.0f;

    if (infoContainer.getVisibility() == View.INVISIBLE) {
        infoContainer.setVisibility(View.VISIBLE);
        ViewAnimationUtils.createCircularReveal(infoContainer, cx, cy, 0, radius).start();
    } else {
        Animator reveal = ViewAnimationUtils.createCircularReveal(
                infoContainer, cx, cy, radius, 0);
        reveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                toolbar.setVisibility(View.INVISIBLE);
            }
        });
        reveal.start();
    }
}

In this way you should be able to create your animation. This is the way to use it. Just apply this at your fab onClick() method