2

I am trying to get snackbar below 3 tabs .I have inflated a fragment and implemented coordinator layout within that.I am unable to display snackbar on top below tabs.And getView() displays snackbar at the bottom above floating button and this code How to show Snackbar at top of the screen displays snackbar right on top which I don't want. How to make snackbar display below tabs in fragment?

Code:

 Snackbar snackbar = Snackbar
                        .make(coordinatorLayout_snackbar, “Snackbar”, Snackbar.LENGTH_INDEFINITE)
                        .setAction(“click”, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Log.e(TAG, "button clicked");
                            }
                        });

                snackbar.show();

xml:

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout_snackbar”
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
</android.support.design.widget.CoordinatorLayout>


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>
Community
  • 1
  • 1
jason
  • 3,932
  • 11
  • 52
  • 123

1 Answers1

4

You need to wrap your SwipeRefreshLayout inside the CoordinatorLayout, like this:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout_snackbar”
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

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

In your current layout, the SwipeRefreshLayout is drawn over the CoordinatorLayout, which has height 0, because it has no children and height set as wrap_content.

patloew
  • 1,090
  • 9
  • 13
  • how can I set it on top.It currently does not even display until I scroll down.Its kind of hidden. – jason Jan 24 '16 at 16:35
  • 1
    You have to put the CoordinatorLayout in the Activity layout, so that the whole fragment is contained in the CoordinatorLayout. – patloew Jan 24 '16 at 16:36
  • CAn you please explain by code ? I tried it and it still shows below the layout. – jason Jan 24 '16 at 17:28