user2350138
user2350138

Reputation: 525

Snackbar hides the Floating Action on pressing the action button on it

I am working on Snack bar and Floating Action button. I used Coordinator layout for making the Floating action button to appear/move when snackbar is displayed. The problem is i kept an action for snackbar . When the floating button is tapped , Snackbar is popping up and Floating action button is moving up. And when i pressed the snackbar action item , the floating action button is getting hidden under the child snackbar.

And also if i press floating action button consecutively , then also floating action button is getting hidden.

Following is my code.

activity_main.xml

<RelativeLayout 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"
tools:context="com.example.dev.firsttest.Screen2"
>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary_color"></android.support.v7.widget.Toolbar>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordinatorlayout">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/searchfab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_add_black_24dp"
        app:fabSize="normal">

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

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

MainActivity

Toolbar toolbar;
FloatingActionButton searchfab;
CoordinatorLayout coordinatorLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen2);

    toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorlayout);

    searchfab = (FloatingActionButton)findViewById(R.id.searchfab);
    searchfab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(coordinatorLayout, "This is Snackbar Demo", Snackbar.LENGTH_LONG).setAction("Click", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Snackbar.make(coordinatorLayout, "This is Child Snackbar", Snackbar.LENGTH_LONG).show();
                }
            }).show();
        }
    });


}

Pressing Child action in Snackbar and consecutive taps on Floating action button makes the Floating action button hides back to the Snackbar

Appreciate your help

Thank you

Upvotes: 9

Views: 5442

Answers (2)

1lb3r
1lb3r

Reputation: 508

The answer its here: https://github.com/ggajews/coordinatorlayoutwithfabdemo .

It will move the FAB when the snackbar is shown.

Upvotes: 7

Matthieu
Matthieu

Reputation: 1239

I was not able to reproduce your issue. Here is my code

View.OnClickListener test1 = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(getActivity().findViewById(R.id.snackbarPosition), "test 1", Snackbar.LENGTH_LONG)
                        .setAction(R.string.snackbar_action_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                 Snackbar.make(getActivity().findViewById(R.id.snackbarPosition), "test 2", Snackbar.LENGTH_LONG)
                                        .setActionTextColor(getResources().getColor(R.color.myBlueGreen))
                                        .show();
                            }
                        })
                        .show();
            }
        };

FloatingActionButton button = (FloatingActionButton)streamView.findViewById(R.id.buttonFloat);
button.setOnClickListener(test1); 

XML

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@android:color/white"
    android:id="@+id/snackbarPosition">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/buttonFloat"
        android:src="@drawable/ic_content_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        app:backgroundTint="@color/myBlueGreen"

        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        />

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

Adding a second snackbar "test2" appear as a OnClickListener on the snackbar "test1", for me, replaces the snackbar "test1" by "test2" (and does not hide the Floating action button)

Also, clicking twice on Floating Action Button makes the snackbar "test1" blink, ie appear twice but not two snackbars one on top of each other. The floating action button does not disappear.

In other words I never see a "child" snackbar on top of a "parent" snackbar.

I cannot see a difference between your code and mine. Maybe try copying my code and see if it solves your problem.

Upvotes: 0

Related Questions