Firstly, I\'d like to preface this with my lack of knowledge about the coordinator layout. I\'m merely following tutorials I found online and am curious why my behavior isn\
The reason why it is not working is that view with Behavior
must be a direct child of CoordinatorLayout
. In your case, the hierarchy is: CoordinatorLayout
-> RelativeLayout
-> LinearLayout
(with Behavior
).
I have a layout such as this. There are a few things located in the upper regions, but the bottom contains only a FAB which is deeply nested.
<android.support.design.widget.CoordinatorLayout
android:id="@+id/your_coordinator_id">
<android.support.constraint.ConstraintLayout
app:layout_behavior="com.yourpackage.YourBehavior">
<ScrollView>
...
</ScrollView>
<android.support.design.widget.TextInputLayout>
...
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout>
...
</android.support.design.widget.TextInputLayout>
<!--
Everything "around" the FAB needs to be moved.
-->
<RelativeLayout
android:id="@+id/your_view_id">
<com.github.jorgecastilloprz.FABProgressCircle>
<!--
This is the actual FAB.
-->
<android.support.design.widget.FloatingActionButton/>
</com.github.jorgecastilloprz.FABProgressCircle>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
The FAB is deeply nested.
CoordinatorLayout > ConstraintLayout > RelativeLayout > FABProgressCircle > FAB
However the RelativeLayout
needs to be pushed up by the CoordinatorLayout
when the Snackbar
is shown.
The behavior which will do this is as simple as follows.
package com.yourpackage;
...
public class YourBehavior extends CoordinatorLayout.Behavior<ConstraintLayout> {
public YourBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, ConstraintLayout child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
// Note that the RelativeLayout gets translated.
child.findViewById(R.id.your_view_id).setTranslationY(translationY);
return true;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, ConstraintLayout child, View dependency) {
child.findViewById(R.id.your_view_id).setTranslationY(0.0f);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, ConstraintLayout child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
}
Show the Snackbar
like this.
Snackbar.make(findViewById(R.id.your_coordinator_id), "Message", Snackbar.LENGTH_SHORT).show();
onDependentViewRemoved
needs to be overridden, because when manually dismissing a Snackbar
the CoordinatorLayout
won't trigger moving the translated View
(the FloatingActionButton
and its RelativeLayout
) back to it's original place. Overriding the method we can translate it back to where it was.