Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a view above Snackbar just like FloatingButton

I got a linear layout that I want to move up when a Snackbar appears.

I saw many examples how to do this with FloatingButton, but what about a regular view?

like image 887
Ilya Gazman Avatar asked Oct 19 '15 14:10

Ilya Gazman


People also ask

How do I adjust my snackbar height?

setTextSize(24); //increase max lines of text in snackbar. default is 2. sbNoAct. show(); int height = sbView.

How do I center the Floating Action button on the snackbar?

Please note, the CoordinatorLayout makes the floating action button move up when the SnackBar popup at the screen bottom. The attribute android:scaleType=”center” makes the floating button icon display correctly.

How to add a snack bar to a regular view?

NO need of Co-ordinator layout using a regular view align the snack bar to the bottom of the view, and place the button on top of it, on click of button of whatever your logic is show snackbar or linear layout. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.

How to show snackbar at the top of the Fab?

One can observe below code that SHOW SNACKBAR button onclick listener we have set the AnchorView to the parent FAB. So that the parent FAB will be visible, even after the Snackbar pops up, and the Snackbar will be shown at the top of the FAB.

How to display the floating button icon correctly in Android?

The attribute android:scaleType=”center” makes the floating button icon display correctly. 4. Content Layout Xml File. This layout XML file is included by the activity_main.xml file.


1 Answers

I'm going to elaborate on the approved answer because I think there's a slightly simpler implementation than that article provides.

I wasn't able to find a built-in behavior that handles a generic moving of views, but this one is a good general purpose option (from http://alisonhuang-blog.logdown.com/posts/290009-design-support-library-coordinator-layout-and-behavior linked in another comment):

import android.content.Context; import android.support.annotation.Keep; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.view.View;  @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());         ViewCompat.setTranslationY(child, translationY);         return true;     }      //you need this when you swipe the snackbar(thanx to ubuntudroid's comment)     @Override     public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {         ViewCompat.animate(child).translationY(0).start();     } } 

then, in your layout file add a layout_behavior as below:

<LinearLayout     android:id="@+id/main_content"     android:orientation="vertical"     app:layout_behavior="com.example.MoveUpwardBehavior"/> 

where the layout_behavior is the full path to your custom behavior. There's no need to subclass LinearLayout unless you have a specific need to have a default behavior, which seems uncommon.

like image 78
Travis Castillo Avatar answered Oct 07 '22 01:10

Travis Castillo