0

I am trying to dismiss a Snackbar when a condition is satisfied.

snackbar.setAction("Retry", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isConnected(getApplicationContext())){
                    toggleButtonsState(true);
                    snackbar.dismiss();
                } else {
                    snackbar.show();
                }
            }
        });
        snackbar.show();

However, the Snackbar implementation do automatic dismiss of it when the action is clicked.

public Snackbar setAction(CharSequence text, final OnClickListener listener) {
    TextView tv = this.mView.getActionView();
    if(!TextUtils.isEmpty(text) && listener != null) {
        tv.setVisibility(0);
        tv.setText(text);
        tv.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                listener.onClick(view);
                Snackbar.this.dismiss();
            }
        });
    } else {
        tv.setVisibility(8);
        tv.setOnClickListener((OnClickListener)null);
    }

    return this;
}

Generally, is there a way to modify a compiled gradle library so as to cope with my needs. ie. to remove this line Snackbar.this.dismiss();. I know I can search for the source and put in in libs folder and then I can modify it but is there a way to do so rather than this?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
aselims
  • 1,843
  • 1
  • 17
  • 19

1 Answers1

0

Unfortunately, you can't because Snackbar class is final and cannot override methods so it's behavior.

What you can try, is setting View.OnTouchListener to Snackbar view and prevent it getting any touch events only for the action view. I'm not sure if snackBar.getView() returns whole snackbar including the action views.

snackBar.getView().setOnTouchListener(new View.OnTouchListener(){
     @Override
     public boolean onTouch (View v, MotionEvent event){
         if(v instanceof Button) {
            //This is the Action view event, do your logic.
             if(isConnected(getApplicationContext())){
                    toggleButtonsState(true);
                 return false;
              }
             return true;
         }
         //not action view, proceed normally
         return false;
      });
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148