0

I want to disable swipe to dismiss behavior of snackbar i have tried lot of things not worked any for me , any one suggest how can i achieve this, Here is what i have tried

final Snackbar snackbar = Snackbar
                .make(main_content, getResources().getString(R.string.no_internet), Snackbar.LENGTH_INDEFINITE);
        snackbar.setActionTextColor(Color.BLACK);

        View snackbarView = snackbar.getView();
        snackbarView.setBackgroundColor(Color.WHITE);
        TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.BLACK);
       // textView.setTypeface(fontLight);
        snackbar.setAction("RETRY", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                apiAccess();
            }
        });


        snackbar.show();

This is my snackbar code i have tried two things :

1

((android.support.design.widget.CoordinatorLayout.LayoutParams) snackbar.getView().getLayoutParams()).setBehavior(null);

after snackbar.show(); method but didn't worked.

2

main_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        ViewGroup.LayoutParams lp = main_content.getLayoutParams();
                        if (lp instanceof CoordinatorLayout.LayoutParams) {
                            ((CoordinatorLayout.LayoutParams) lp).setBehavior(new DisableSwipeBehavior());
                            main_content.setLayoutParams(lp);
                        }
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            main_content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        } else {
                            //noinspection deprecation
                            main_content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }
                    }
                });

        public class DisableSwipeBehavior extends SwipeDismissBehavior<Snackbar.SnackbarLayout> {
        @Override
        public boolean canSwipeDismissView(@NonNull View view) {
            return false;
        }
    }

In Above code main_content is my CoordinatorLayout.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Rajesh Koshti
  • 572
  • 1
  • 7
  • 25

0 Answers0