blackHawk
blackHawk

Reputation: 6307

How to make snack bar and fab to move together

I want to move snack bar and fab to move together like happen in default behaviour but by default it happens only when we click on fab, the fab makes place for snackbar and fab itself moves up,

But want it to happen when some other event happen like other button liked on layout not fab itself, how can i trigger fab to move up and make place for snackbar when other button on layout clicks

Thanks

Onclick of button:

 public void onClick(View view) {
             if(spinner.getText().length() <= 0){
                 showListMenu(spinner);
             }else{
                 showSnack("Added to bag we will hold it for 1 hour!", view.getRootView());
             }
        }

Inside showSnack:

 Snackbar.make(view.getRootView(), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

    final Animation myAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.bounce);
    double animationDuration = 2 * 1000;
    myAnim.setDuration((long)animationDuration);

    MyBounceInterpolator interpolator = new MyBounceInterpolator(0.20, 20.00);

    myAnim.setInterpolator(interpolator);
    fab.startAnimation(myAnim);

Upvotes: 2

Views: 1888

Answers (2)

blackHawk
blackHawk

Reputation: 6307

Just anchor fab with snackbar like this:

Snackbar.make(view.findViewById(R.id.fab), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();

Upvotes: 3

MathankumarK
MathankumarK

Reputation: 2905

Use CoordinatorLayout that's enough, no need to change anything on Java code. Add your button inside your CoordinatorLayout

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

For both click event SnackBar and Fab moves up smoothly

  fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

Upvotes: 2

Related Questions