Snackbar Not showing up with fragment and swiperefresh layout

673 views Asked by At

I want to show snackbar from fragment and layout xml goes like this

activity_main.xml

<android.support.design.widget.CoordinatorLayout 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"
    android:id="@+id/snackbar_holder"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay" />

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

    <include layout="@layout/content_main" />

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/fab_refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="108dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_refresh"
        app:fab_colorNormal="@color/white"
        app:fab_colorPressed="@color/greyLight"
        app:fab_colorRipple="@color/grey_dull"
        app:fab_hideAnimation="@anim/scale_down"
        app:fab_showAnimation="@anim/scale_up"
        app:fab_size="mini" />
    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/white_like_bg_darker"
        app:layout_anchor="@+id/bnv_section_selector"
        app:layout_anchorGravity="top" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bnv_section_selector"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        app:itemBackground="@color/white"
        app:itemIconTint="@drawable/bottom_navigation_selector"
        app:itemTextColor="@drawable/bottom_navigation_selector"
        app:menu="@menu/bottom_navigation_main" />

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

content_main.xml

<FrameLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">


</FrameLayout>

Fragment is inflated in FrameLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_like_bg">

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

        <HorizontalScrollView
            android:id="@+id/buttonLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/strip_height_price"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="1dp"
            android:background="@color/white_like_bg"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/strip"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="horizontal" />


        </HorizontalScrollView>

        <View
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="@dimen/element_padding"
            android:layout_below="@+id/buttonLayout"
            android:background="@drawable/shadown" />


        <RelativeLayout
            android:id="@+id/ll_filter_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:visibility="gone">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="8dp"
                android:layout_toLeftOf="@+id/iv_cross"
                android:text="Filtered Results" />

            <ImageView
                android:id="@+id/iv_cross"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:background="?attr/selectableItemBackground"
                android:padding="4dp"
                android:src="@drawable/ic_close" />
        </RelativeLayout>
    </RelativeLayout>

    <RadioGroup
        android:id="@+id/rg_logistics_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/swipe_view_holder"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:visibility="gone">

        <RadioButton
            android:id="@+id/rb_logistics_tab_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/checkable_bg_selector"
            android:button="@null"
            android:paddingBottom="8dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="8dp"
            android:text="@string/iron_ore"
            android:textColor="@color/checkable_text_color_selector" />

        <RadioButton
            android:id="@+id/rb_logistics_tab_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/checkable_bg_selector"
            android:button="@null"
            android:paddingBottom="8dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="8dp"
            android:text="@string/coal"
            android:textColor="@color/checkable_text_color_selector" />

        <RadioButton
            android:id="@+id/rb_logistics_tab_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/checkable_bg_selector"
            android:button="@null"
            android:paddingBottom="8dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="8dp"
            android:text="@string/met_coke"
            android:textColor="@color/checkable_text_color_selector" />

    </RadioGroup>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/rg_logistics_selector">

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


            <se.emilsjolander.stickylistheaders.StickyListHeadersListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/head_padding"
                android:layout_marginRight="@dimen/head_padding"
                android:layout_marginTop="@dimen/head_padding"
                android:clipToPadding="false"
                android:nestedScrollingEnabled="true"
                android:paddingBottom="@dimen/row_height"
                android:visibility="visible" />
        </android.support.v4.widget.SwipeRefreshLayout>

        <TextView
            android:id="@+id/message_textview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:clickable="true"
            android:gravity="center"
            android:padding="@dimen/outer_padding"
            android:textColor="@color/text_grey"
            android:textSize="@dimen/textSizeHighlight"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/progresslayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:clickable="true"
            android:visibility="gone">

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>


    </FrameLayout>

</RelativeLayout>

I have added snackbar. But it's not showing up. Here's code i've used to show snackbar

View parent = getActivity().findViewById(R.id.snackbar_holder);
            Snackbar.make(parent,
                    "Seems you are logged out", BaseTransientBottomBar.LENGTH_LONG).show();

Even I have tried to show it in MainActivity itself but no luck... Am i missing anything ?

2

There are 2 answers

1
Sunil P On

since your parent layout is CoordinatorLayout to show snackbar you have to initialize CoordinatorLayout first

CoordinatorLayout coordinatorlayout = (CoordinatorLayout)view.findViewById(R.id.snackbar_holder)
Snackbar.make(coordinatorlayout, "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();
1
AskNilesh On

try this

CoordinatorLayout parent = (CoordinatorLayout)getActivity().findViewById(R.id.snackbar_holder)
Snackbar snackbar = Snackbar.make(parent , "Seems you are logged out", Snackbar.LENGTH_SHORT);
  View sbView = snackbar.getView();
  TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
  textView.setTextColor(Color.YELLOW);
  snackbar.show();