0

I have button instead of floating action button i want to anchor it with snack bar as it anchor with floating action bar like floating action button moves up when snack bar is shown, with floating action bar we do it like this:

Snackbar.make(fab, s, Snackbar.LENGTH_SHORT);

how can I do with simple button as snackbar and button don't know snackbar presence and vice versa and moves independently

Thanks

blackHawk
  • 6,047
  • 13
  • 57
  • 100

2 Answers2

1

The Docs say this

Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.

So as you long as your button is directly under the Coordinator Layout and you pass the button to the Snackbar like this

Snackbar.make(yourButton, s, Snackbar.LENGTH_SHORT); . The moving of widgets upwards should work.

Dishonered
  • 8,449
  • 9
  • 37
  • 50
0

You have to attach a Behavior to the View you want to move along with Snakebar movement

In here I make a custom View and attach the behavior to it

@CoordinatorLayout.DefaultBehavior(CustomView.Behavior.class)
public class CustomView extends View {
    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public static class Behavior extends CoordinatorLayout.Behavior<CustomView> {

        public Behavior() {
            super();
        }

        public Behavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp) {
            if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
                // This setting makes it works (learn from FloatingActionButton.Behavior)
                lp.dodgeInsetEdges = Gravity.BOTTOM;
            }
        }

        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, CustomView child,
                                              View dependency) {
            return false;
        }
    }
}

Use this CustomView in your layout and it should work

The FloatingActionButton does the same thing, it has an inner Behavior which do many more things with the shadow padding, show/hide base on AppBarLayout movement. Every movement in CoordinatorLayout base on its Behavior mechanism

Ref here: http://saulmm.github.io/mastering-coordinator

P/S: About Snakebar.make usage, pass the CoordinatorLayout into view parameter, don't pass any other components, that will save the system performance. If you look at the Snakebar code, it has a loop, uses the view parameter to find the root view. The root view is what it wants:

private static ViewGroup findSuitableParent(View view) {
    ViewGroup fallback = null;
    do {
        if (view instanceof CoordinatorLayout) {
            // We've found a CoordinatorLayout, use it
            return (ViewGroup) view;
        } else if (view instanceof FrameLayout) {
            if (view.getId() == android.R.id.content) {
                // If we've hit the decor content view, then we didn't find a CoL in the
                // hierarchy, so use it.
                return (ViewGroup) view;
            } else {
                // It's not the content view but we'll use it as our fallback
                fallback = (ViewGroup) view;
            }
        }

        if (view != null) {
            // Else, we will loop and crawl up the view hierarchy and try to find a parent
            final ViewParent parent = view.getParent();
            view = parent instanceof View ? (View) parent : null;
        }
    } while (view != null);

    // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
    return fallback;
}
Tam Huynh
  • 2,026
  • 1
  • 16
  • 20
  • In code you provided where its state that button or view should move up with snackbar? – blackHawk Jun 01 '18 at 07:21
  • I updated the code with a comment, `Gravity.BOTTOM` is where it happens – Tam Huynh Jun 01 '18 at 07:26
  • Is `CoordinateLayout` your root layout? And do you pass it as param to `Snakebar.make(coordinatorLayout, ...)` – Tam Huynh Jun 01 '18 at 08:41
  • yes i did, as fab.getRootView(); fab is custom button also i extend Button instead of View in the code you provided – blackHawk Jun 01 '18 at 08:48
  • Ok the problem is you use `getRootView`, which will return the PhoneWindow.DecorView, the outermost container. This is the container of `CoordinatorLayout` so it cannot find one using current logic. Passing exactly the `CoordinatorLayout`, or the button itself should fix it – Tam Huynh Jun 01 '18 at 08:58
  • could you see my other post? https://stackoverflow.com/questions/50597274/fragment-to-fragment-shared-element-transition-is-not-working-when-exit – blackHawk Jun 01 '18 at 09:45