Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snackbar and FloatingActionButton bug

I have a FAB button in a Coordinator Layout, and I have set snackbars to pop up in the Coordinator Layout. However if a snackbar is visible and I execute an action that triggers another snackbar while the first one is still visible, the FAB button glitches and falls behind the snackbar. Instead of moving with the snackbar.

I have the Coordinator Layout within a Relative Layout though with an adview below the Coordinator Layout.

This is the layout.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background">

<RelativeLayout
    android:id="@+id/circle2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/container2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView"
        android:background="#FFF3E0"
        tools:context=".MainActivity"
        android:orientation="vertical">

        <SurfaceView
            android:layout_width="0px"
            android:layout_height="0px"
            android:visibility="gone" />
        <include
            android:id="@+id/circle"
            layout="@layout/periodictablelayout" />


        <include
            android:id="@+id/toolbar_actionbar"
            layout="@layout/toolbar_default"

            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <FrameLayout
            android:id="@+id/container"
            android:layout_width="fill_parent"

            android:layout_height="fill_parent"
            android:layout_marginTop="?android:attr/actionBarSize"

            />
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/periodbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:elevation="3dp"
            android:onClick="onClick"
            app:layout_anchorGravity="bottom|right|end"
            app:layout_anchor="@+id/container2"
            android:src="@drawable/ic_view_comfy_white_24dp"
            app:backgroundTint="@color/accent"
            android:layout_gravity="bottom|end"
            app:layout_behavior="ibochemistry.titomo.timetoolay.google.com.ibchemistry.ScrollAwareFABBehaviour" />


    </android.support.design.widget.CoordinatorLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
        app:layout_anchorGravity="bottom" />
</RelativeLayout>
<!-- android:layout_marginTop="?android:attr/actionBarSize"-->
<fragment
    android:id="@+id/fragment_drawer"
    android:name="ibo.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
    app:layout="@layout/fragment_navigation_drawer"

    tools:layout="@layout/fragment_navigation_drawer" />

like image 221
overhound Avatar asked Oct 31 '22 20:10

overhound


1 Answers

How about moving the FAB up with the snackBar. I guess that will fix your problem. Whenever a snackBar is shown do this ,

SnackbarManager.show(
Snackbar.with(getApplicationContext()) // context
    .text("This will do something when dismissed") // text to display
    .eventListener(new EventListener() {
        @Override
        public void onShow(Snackbar snackbar) {
            myFloatingActionButton.moveUp(snackbar.getHeight());
        }
        @Override
        public void onShown(Snackbar snackbar) {
            Log.i(TAG, String.format("Snackbar shown. Width: %d Height: %d Offset: %d",
                    snackbar.getWidth(), snackbar.getHeight(),
                    snackbar.getOffset()));
        }
        @Override
        public void onDismiss(Snackbar snackbar) {
            myFloatingActionButton.moveDown(snackbar.getHeight());
        }
        @Override
        public void onDismissed(Snackbar snackbar) {
            Log.i(TAG, String.format("Snackbar dismissed. Width: %d Height: %d Offset: %d",
                                snackbar.getWidth(), snackbar.getHeight(),
                                snackbar.getOffset()));
        }
    }) // Snackbar's EventListener
, this); // activity where it is displayed

Let me know if it works for you...

like image 193
Rohit Jagtap Avatar answered Nov 04 '22 07:11

Rohit Jagtap