Reputation: 101
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
Upvotes: 0
Views: 7188
Reputation: 148
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();
}
Upvotes: 3
Reputation: 5819
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>
Upvotes: 0
Reputation: 569
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"
Upvotes: 0
Reputation: 1944
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.
Upvotes: 0
Reputation: 2535
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();
Upvotes: 0