Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing Snackbar shows hidden bottom sheet as well?

Tags:

android

I have an activity with a coordinator layout. Inside the coordinator layout is a view which inherits the default bottom sheet layout behavior for the Google Support Library Bottom Sheet. The issue is that when I call Snackbar.show() with the coordinator layout as the view, the bottomsheet pops up as well.

Here is the call to show snackbar:

Snackbar.make(coordinatorLayout, R.string.status_image_saved, 
Snackbar.LENGTH_SHORT).show();

Here is the layout:

<android.support.design.widget.CoordinatorLayout>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.Toolbar />

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

    <android.support.v7.widget.RecyclerView />

</android.support.constraint.ConstraintLayout>

<LinearLayout
    android:id="@+id/attachment_selector"
    android:layout_width="match_parent"
    android:layout_height="480dp"
    android:background="@color/colorBack"
    android:elevation="10dp"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="@dimen/bottom_sheet_start"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <android.support.design.widget.TabLayout
        android:id="@+id/attachment_selector_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed" />

    <android.support.v4.view.ViewPager
        android:id="@+id/attachment_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>
like image 345
Nick Mowen Avatar asked Mar 30 '17 20:03

Nick Mowen


2 Answers

It's a late answer but might be helpful for others. If you are using a Dialog or BottomSheetDialog you can show a Snackbar providing the top-level window decor view.

Here's my solution in Kotlin:

    Snackbar.make(
            dialog.window.decorView,                   // important part
            "your-string",
            Snackbar.LENGTH_SHORT
    ).show()
like image 114
Ryan Amaral Avatar answered Oct 13 '22 00:10

Ryan Amaral


The solution using dialog.window.decorView appeared over Android's soft navigation keys for me. Instead I shifted the Snackbar's elevation to be greater than the BottomSheetDialog's elevation.

This keeps the Snackbar at the correct position in the y-axis and the new elevation brings it above the bottom sheet:

Snackbar.make(binding.root, "Example", Snackbar.LENGTH_LONG)
    .apply {
        view.elevation = 1000F
    }.show()
like image 45
Patrick Avatar answered Oct 13 '22 00:10

Patrick