androidmaterial-designandroid-snackbarsnackbarattachedbehaviors

Move Button down when dissmising snackbar manually


So I have following basic code which makes sure that my button goes up when a snackbar appears:

public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
    private static final boolean SNACKBAR_BEHAVIOR_ENABLED;

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    static {
        SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
    }
}

My customlinearlayout:

public class CustomLinearLayout extends LinearLayout implements CoordinatorLayout.AttachedBehavior {

    MoveUpwardBehavior mb = new MoveUpwardBehavior();
    public CustomLinearLayout(Context context) {
        super(context);
    }

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

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @NonNull
    @Override
    public CoordinatorLayout.Behavior getBehavior() {
        return mb;
    }
}

And last but not least my layout:

<?xml version="1.0" encoding="utf-8"?>
 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 app:layout_behavior=".pkgActivity.MoveUpwardBehaviour"
 tools:context=".pkgTestforend.DriverListFragment">


    <ListView
        android:id="@+id/listAllDrivers"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>


    <com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout
        android:layout_width="match_parent"
        android:id="@+id/cusLL"
        android:layout_height="match_parent"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/btnOpenDriverAddFragment2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_person_add_24"
                />

        </RelativeLayout>

    </com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

This works perfectly fine, however the issue is that button remains up, when the snackbar gets dissmissed manually:

remains up

In order to solve the issue I tried the following:

Snackbar kd = Snackbar.make(customLinearLayout, "Text to display", Snackbar.LENGTH_LONG)
    .addCallback(new Snackbar.Callback() {

        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            if (event == Snackbar.Callback.DISMISS_EVENT_SWIPE) {
                btnOpenDriverAddFragment2.setTranslationY(90); //lower button down manually!
                }                                        
            }

            @Override
            public void onShown(Snackbar snackbar) {
            }
        });

kd.show();

However, this doesnt work very well, as the snackbar seems still be visible/button gets covered by the dissmissed snackbar?

dissmised

Why is it so, that the snackbar remains basicallyvisible but dissmissed?


Solution

  • Changing btnOpenDriverAddFragment2.setTranslationY(90); with customLinearLayout.setTranslationY(0); will solve your issue.

    Since not only the button gets moved up but also the custom linear layout which is also the parent of the button u just need to reset the y position of the parent. U just change the y value of the button, but u forgot about the linearlayout.