[Android] Arbitrary View Evasion when Snackbar pops up

Keywords: Android xml encoding Fragment

brief introduction

Snackbar is a control that pops up messages at the bottom, similar to Toast.

Basic use:

Snackbar.make(view, message_text, duration)
   .setAction(action_text, click_listener)
   .show();

We know that when the root layout is Coordinator Layout and the Coordinator Layout's behavior is set, Snackbar pops up, the fab (floating button) will move up automatically to prevent occlusion.

The code is as follows:

Layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    <!-- Note the settings here layout_behavior -->
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".fragment.TodoFragment">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!-- Other View Layout -->
    ...

    </androidx.constraintlayout.widget.ConstraintLayout>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="false"
        android:clickable="true"
        app:backgroundTint="@color/blue"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_add_d" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

Note: I use the AndroidX support library here. If you do not migrate to AndroidX, you need to change the reference path of the control.

Then call the display Snackbar in activity:

Snackbar.make(coordinator_layout, "Snackbar Pop-up information",  Snackbar.LENGTH_SHORT)
   .show();

Effect (Constraint Layout dodge has been implemented here):

Dodge effect

Implementing Arbitrary View Evasion

So how to dodge any View?

Looking at Stack Overflow, it was found that the best practice should be to implement a common behavior.

No more nonsense, just code (Java):

@Keep
public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {

    public MoveUpwardBehavior() {
        super();
    }

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

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

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

    // Rewrite this method to achieve sliding clearance messages
    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
        ViewCompat.animate(child).translationY(0).start();
    }
}

Then in the layout file, add the layout_behavior to the View you want to dodge:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    <!-- That's the sentence. Be careful to have a complete package name. -->
    app:layout_behavior="net.sarasarasa.lifeup.base.MoveUpwardBehavior">

    <!-- Other View Layout -->
    ...

</androidx.constraintlayout.widget.ConstraintLayout>

Quick access to quotations with full registration in AS: right-click your class file and select Copy Reference.

Yes, that's it. Let's finish the test.

Reference resources

https://stackoverflow.com/questions/33217241/how-to-move-a-view-above-snackbar-just-like-floatingbutton

Posted by Jorn TK on Fri, 19 Apr 2019 15:42:33 -0700