Push other view up when Snackbar showing

CoordinatorLayout & Floating Action Button

  • define the layout

    <!--?xml version="1.0" encoding="utf-8"?-->
    <android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"/>
    
    </android.support.design.widget.CoordinatorLayout>
    
  • show Snackbar

    Snackbar.make(view, messageResId, LENGTH_SHORT)
                .show();
    

Then, this will push up the button when Snackbar is displayed.

Android Design Support Library (100 Days of Google Dev)

Not native Floating Action Button or thirty parity UI

  • define the layout with TextView
<!--?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:text="Push me up please"
    android:layout_margin="16dp"
    app:layout_behavior="com.hsy.PushUpBehavior"/>

</android.support.design.widget.CoordinatorLayout>
  • custom the behavior class
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class PushUpBehavior extends CoordinatorLayout.Behavior<TextView> {

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

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

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

Push up whole layout

  • remove the constructor of PushUpBehavior class

    public class PushUpBehavior extends CoordinatorLayout.Behavior<View> {
    
      @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, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
      }
    }
    
  • custom the ConstraintLayout with behavior

    @CoordinatorLayout.DefaultBehavior(PushUpBehavior.class)
    public class PushUpConstraintLayout extends ConstraintLayout {
    
      public PushUpConstraintLayout(Context context) {
        super(context);
      }
    
      public PushUpConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public PushUpConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      }
    }
    
  • define the layout

    <?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">
    
      <com.hsy.PushUpConstraintLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
          android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="first text"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          tools:text="first text"/>
    
        <android.support.v7.widget.RecyclerView
          android:id="@+id/txsContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingLeft="10dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/text"
          android:paddingRight="10dp"/>
    
      </com.hsy.PushUpConstraintLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

This will push up whole ConstraintLayout when Snackbar showing.

MusicGenre

results matching ""

    No results matching ""