Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Displaying Snackbar Inside Fragment

I am running into some issues with displaying a snackbar inside a fragment. I can set one up fine for an activity but when I try in a fragment it just doesnt appear and no error is given.

Snackbar inside fragment

Snackbar snackbar = Snackbar.make(rootView, "Snackbar test", 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();   

rootView in fragment

    rootView = inflater.inflate(R.layout.fragment_possible_recipes, container, false);

Fragments XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="50dp"
    android:src="@drawable/no_result"
    android:visibility="gone"
    android:id="@+id/imageview"
    android:layout_gravity="center"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/filterButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="63dp"
    android:layout_marginRight="16dp"
    android:clickable="true"
    android:src="@drawable/ic_filter"
    app:backgroundTint="@color/floatingButton" />


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

Main Activity XML

<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout2">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

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


<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.rory.pocketchef.Fragments.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
like image 280
JJSmith Avatar asked Mar 04 '16 11:03

JJSmith


People also ask

How to display snackbar in fragment android?

To show an Android Snackbar message from an Activity or Fragment, use Java code like this: Snackbar. make(view, "going to: " + url, Snackbar.

How do you show snackbar in Kotlin?

This example demonstrates how to use Snackbar in Android Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I get view on snackbar?

make(parentlayout, "This is main activity", Snackbar. LENGTH_LONG) . setAction("CLOSE", new View. OnClickListener() { @Override public void onClick(View view) { } }) .

How do I use snackbar in service?

Import the snackBarService and inject it inside the constructor of the component, in which you want to use the Snackbar. This will create an instance of the service say snackBService. Now call the openSnackBar function wherever it is required, with the help of snackBService.


3 Answers

You can get it done far easily just by using it like following.

Snackbar.make(getActivity().findViewById(android.R.id.content),
           "text to show", Snackbar.LENGTH_LONG).show();

This should work in anywhere within a Fragment.

PS. I know this is old but I leave it here anyway for future reference.

like image 167
fluffyBatman Avatar answered Dec 09 '22 23:12

fluffyBatman


try this way

You pass a View that is not a CoordinatorLayout the Snackbar will walk up the tree until it finds a CoordinatorLayout or the root of the layout.

  rootlayout = (CoordinatorLayout) rootView.findViewById(R.id.coordinatorLayout);

and Snackbar

  Snackbar snackbar = Snackbar.make(rootlayout , "Snackbar test", 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(); 
like image 20
saeed Avatar answered Dec 10 '22 00:12

saeed


Snackbar.make(requireActivity().findViewById(R.id.constraintLayout),"your message", Snackbar.LENGTH_LONG)
.show();

android:id="@+id/constraintLayout"

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
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=".YourFragment">
like image 44
Meysam Keshvari Avatar answered Dec 10 '22 00:12

Meysam Keshvari