Search code examples
androidandroid-custom-viewandroid-snackbar

android: custom snackbar bar displays default snack bar in the background


I have a custom snack bar with a custom layout.But it doesn't display properly.It shows the default snackbar as well in the background: Below is my code:

    import android.support.annotation.NonNull;
    import android.support.design.widget.BaseTransientBottomBar;
    import android.support.design.widget.Snackbar;
    import android.support.v7.widget.AppCompatTextView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class CustomSnackbar extends BaseTransientBottomBar<CustomSnackbar> {

    protected CustomSnackbar(@NonNull ViewGroup parent,
                             @NonNull View content, @NonNull 
        android.support.design.snackbar.ContentViewCallback contentViewCallback) {
        super(parent, content, contentViewCallback);
    }

    private static class ContentViewCallback
            implements android.support.design.snackbar.ContentViewCallback {

        // view inflated from custom layout
        private View view;

        public ContentViewCallback(View view) {
            this.view = view;
        }

        @Override
        public void animateContentIn(int delay, int duration) {
            // TODO: handle enter animation
        }

        @Override
        public void animateContentOut(int delay, int duration) {
            // TODO: handle exit animation
        }
    }

    public static CustomSnackbar make(ViewGroup parent, int duration,String text) {
        // inflate custom layout
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.custom_snackbar, parent, false);
        //Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)view;
        //layout.setPadding(0, 0, 0, 0);//set padding to 0
        AppCompatTextView textView = (AppCompatTextView) view.findViewById(R.id.textview_snackbar_text);
        textView.setText(text);
        // create with custom view
        ContentViewCallback callback= new ContentViewCallback(view);
        CustomSnackbar customSnackbar = new CustomSnackbar(parent, view, callback);
        customSnackbar.setDuration(duration);
        return customSnackbar;
    }
}

Below is my custom layout for my custom snackbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp"
android:padding="0dp"
android:background="@android:color/transparent"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/button_green"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    >
<android.support.v7.widget.AppCompatTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textview_snackbar_text"
    android:textColor="@color/white"
    android:text=""
    android:textSize="@dimen/extra_small_text_size"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    />
</LinearLayout>
</android.support.v7.widget.CardView>

This is how I use my custom snackbar from BaseActivity:Im using it in BaseActivity and android.R.id.content so that I can call my custom snackbar from all the activities.

public void showSnackbar(String message) {
        CustomSnackbar.make(findViewById(android.R.id.content),
                CustomSnackbar.LENGTH_LONG,message).show();
}

But when I call this method, this is how it shows:

enter image description here

I have explored many stack over flow questions like Custom Snackbar doesn't work properly and Snackbar from custom class not showing, but those solutions doesn't work for me.

Any ideas or insights will be really helpful.

Edit1:As mentioned in the answer given,;

Inside my CustomSnackbar's make method: I gave the follwing:

    view.setBackgroundColor(parent.getContext().getResources().getColor(R.color.design_snackbar_background_color));

And it looks like the below:

enter image description here

My cardview's behaviour is lost now: Is there any way to bring back my cardview's corner radius as well and the width to be stretched to maximum !!??


Solution

  • override the following value

    <color name="design_snackbar_background_color" tools:override="true">@color/transparent</color>
    

    or

        Snackbar snackbar = Snackbar.make((ViewGroup) findViewById(android.R.id.content), "", Snackbar.LENGTH_LONG);
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
        TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
        textView.setVisibility(View.INVISIBLE);
        LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
        View snackView = mInflater.inflate(R.layout.snackbar, null);
        TextView textViewTop = (TextView) snackView.findViewById(R.id.textview_snackbar_text);
        textViewTop.setText("ssdfds");
        textViewTop.setTextColor(Color.WHITE);
        layout.setPadding(0,0,0,0);
        layout.addView(snackView, 0);
        layout.setBackgroundColor(getResources().getColor(R.color.transparent));
        snackbar.show();
    

    in your styles.xml paste the below code

     <style name="MyRoundSnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
            <item name="android:layout_margin">32dp</item>
        </style>
    

    and then in your theme paste this line

    <item name="snackbarStyle">@style/MyRoundSnackbar</item>
    

    Also paste this line in your xml

    <color name="transparent">#00FFFFFF</color>
    

    enter image description here