-1

I made a function of Snackbar :

 void openSnackbar(String title){
            Snackbar
                    .make(findViewById(R.id.coordinatorLayout),
                            title,
                            Snackbar.LENGTH_LONG)
                    .setAction("",this)
                    .show();
    }

But inside seAction() method second perameter as this giving error you can't resolve method setAction(java.util.String,package.....)

How to solve this problem?

Yeahia2508
  • 7,526
  • 14
  • 42
  • 71

2 Answers2

2

But inside seAction() method second perameter as this giving error you can't resolve method setAction(java.util.String,package.....)

the second parameter of setAction is an instance of a class that implements View.OnClickListener. If the compiler is complaining about it , it is because your class is not implementing that interface.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Check the official javadoc.

  setAction (CharSequence text, View.OnClickListener listener)

  Parameters:
    text        Text to display
    listener    callback to be invoked when the action is clicked

You can use something like this:

  Snackbar
       .make(findViewById(R.id.coordinatorLayout),
             title,
             Snackbar.LENGTH_LONG)
       .setAction("", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Your code
                }
            })
       .show();

Otherwise you can use:

  Snackbar
       .make(findViewById(R.id.coordinatorLayout),
             title,
             Snackbar.LENGTH_LONG)
       .setAction("", myOnClickListener)
       .show();

myOnClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Your code
                    }
                };

If you would like to use

.setAction("",this)

your class have to implement the View.OnClickListener interface.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841