Snackbar and FloatingButton Behavior

Snackbar is just an extension of Toast. It is an actionable Toast. Snackbar will stay on the screen forever or for a short duration (Snackbar.LENGTH_INDEFINITE, SnackBar.LENGTH_LONG, Snackbar.LENGTH_SHORT) and we can set action as well. One example that is coming to my mind is, from a list user deleted an item, then we can give user the option to "Undo" using Snackbar.

Now we will come to the implementation part.

Snackbar snackbar = Snackbar.make(recyclerView, "Do you want to undo", Snackbar.LENGTH_LONG).setAction("Undo", snackBarClickListener);
snackbar.setActionTextColor(Color.WHITE);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.DKGRAY);
snackbar.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {

     @Override
     public void onViewAttachedToWindow(View v) {
        // write your code here
     }

     @Override
     public void onViewDetachedFromWindow(View v) {
        // write your code here
     }
});
Now about the floating button. Floating button is like when the snackbar appears, floating button floats. We will see the implementation. Here the floating action button is bottom right side. The xml to be defined in the layout is,

<android.support.design.widget.FloatingActionButton
        android:id="@+id/bottom_right_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:src="@drawable/foooooo"
        app:backgroundTint="#some color"
        app:elevation="6dp"
        app:fabSize="normal"
        app:rippleColor="# some color"
        app:layout_behavior="com.smartandroidians.FloatingActionButtonBehavior"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        app:layout_anchorGravity="bottom|right|end" />
Now the class of FloatingActionButton is as follows,
public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior {

    public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
    }

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

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }
}
This is in the new android design support library. In gradle file, add this line as
dependencies {
  compile 'com.android.support:design:22.2.1'
}
Then if you are using proguard file, then all these lines, otherwise FloatingActionButtonBehavior class won't accept the proguard.
-keepclasseswithmembers class * {
    native ;
}

-keepclasseswithmembers class * {
    public (android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public (android.content.Context, android.util.AttributeSet, int);
}
If you want to know more about proguard settings, please check here

One last point is, for FloatingActionButton should be in CoordinatorLayout. the xml is as,
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
That's it !!! Enjoy Coding !!!

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Open Gallery and get the selected image in appplication