简体   繁体   中英

Android : snackbar not working in fragment class

I want to use snackbar in my app so I set below code for that

public class FragmentAddProperty extends Fragment
{
   RelativeLayout mRelative 
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)  {
        view = inflater.inflate(R.layout.layout_1,
                container, false);
    mRelative = (RelativeLayout) view.findViewById(R.id.edt_pro_city);
    Snackbar.make(mRelative, "No images.", Snackbar.LENGTH_LONG).show();
    }

}

EDIT

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edt_pro_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8"
android:orientation="vertical" >



        <ImageView
            android:id="@+id/sample_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_list"
            android:visibility="visible" />
            </RelativeLayout>

As result I got following error

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.design.widget.Snackbar$SnackbarLayout

How can I solve this ?

All suggestion are appreciable

You can use snackbar in Frgamnet.just use object of ViewGroup.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
 {
  Snackbar.make(container, "Please Check your Internet Connection",Snackbar.LENGTH_LONG).show(); 
 }

In Snakebar, you have to pass view or parent layout view. So just replace your id with view and replace your code with this .

Snackbar.make(view, "No images.", Snackbar.LENGTH_LONG).show();

For example, say your existing layout is a RelativeLayout you could add a CoordinatorLayout to relative layout as follows:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/myCoordinatorLayout"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
</android.support.design.widget.CoordinatorLayout>

Then, make sure you pass the CoordinatorLayout as the first argument of the Snackbar.make() command.

final View viewPos = findViewById(R.id.myCoordinatorLayout);    
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
                            .setAction(R.string.snackbar_action_undo, showListener)
                            .show();

This will result in the Snackbar being shown at the bottom of the CoordinatorLayout provided to the make() function.

If 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.

I worked a lot on google to try to display the snackbar in android fragments, But it;s not happening because I need to add parent view as "CoordinatorLayout".

In CoordinatorLayout I used my entire layout structre and I gave the CoordinatorLayout as a parent view for snack bar.

like following:

=> In .xml file

=> Java class file

CoordinatorLayout parentLayout = (CoordinatorLayout)rootView.findViewById(R.id.parentLayout);

    private void showInternetAlert(){

    Snackbar snackbar = Snackbar
            .make(parentLayout, "No internet connection!", Snackbar.LENGTH_LONG)
            .setAction("RETRY", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                }
            });

    // Changing message text color
    snackbar.setActionTextColor(Color.RED);

    // Changing action button text color
    View sbView = snackbar.getView();
    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
    textView.setTextColor(Color.YELLOW);

    snackbar.show();

}

It's working fine for me.

The issue got at in xml file we need to place parent view as "CoordinatorLayout"

Snackbar wont work without CordinatorLayout , you need to to create your fragment_list.xml like this one, or Snackbar snackbar = Snackbar.make(view, R.string.action, Snackbar.LENGTH_LONG); will end whit java.lang.NullPointerException

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM