-1

I was trying to add Snackbar type message for my actions, When i used Snackbar it gives error cannot resolve make().

Snackbar.make(this,"Field should not be empty ",Snackbar.LENGTH_SHORT).show();

But this gives error

cannot resolve method 'make()'

After googling and checking on SO i found that it somehow works well with normal setOnclicklistener also i tried it and it worked well in setOnclicklistener of a fragment, But i am using setOnclicklistener with lambda expression that is why here i am pretty confused how to use snackbar because the default method giving error.

This is what i am doing

loginButton.setOnClickListener(view -> login());

And here's my login function

private void login() {

    setError();

    String email = loginUserName.getText().toString();
    String password = loginPassword.getText().toString();

    int err = 0;

    if (!validateEmail(email)) {

        err++;
        Snackbar.make(this,"Enter Valid fields",Snackbar.LENGTH_SHORT).show(); //here is the problem
        mTiEmail.setError("Email should be valid !");
    }

    if (!validateFields(password)) {

        err++;
        mTiPassword.setError("Password should not be empty !");
    }

    if (err == 0) {

        loginProcess(email,password);


    } else {

        Toast.makeText(this, "Enter valid details", Toast.LENGTH_SHORT).show();
    }
}

I hope i am using snackbar correctly if not please let me and also tell why it gives error here Cannot resolve make()

Any suggestions or thoughts ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

You can see SnakeBar declaration. First param is View, while you are passing current class object. Perhaps your class is activity.

In Activity

Either pass findViewById(android.R.id.content) which gives root view of current activity. Or pass other view.

In Fragment

Snackbar.make(getActivity().findViewById(android.R.id.content),"Sample", Snackbar.LENGTH_LONG).show();

Here is the Snackbar declaration.

@NonNull
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int duration) {
    ViewGroup parent = findSuitableParent(view);
    if (parent == null) {
        throw new IllegalArgumentException("No suitable parent found from the given view. Please provide a valid view.");
    } else {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        SnackbarContentLayout content = (SnackbarContentLayout)inflater.inflate(hasSnackbarButtonStyleAttr(parent.getContext()) ? layout.mtrl_layout_snackbar_include : layout.design_layout_snackbar_include, parent, false);
        Snackbar snackbar = new Snackbar(parent, content, content);
        snackbar.setText(text);
        snackbar.setDuration(duration);
        return snackbar;
    }
}
Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
1

You might call SnackBar.make() in Activity range. So this is Activity not View. You should change code like this SnackBar.make(loginButton,...).


Update

Compare to Toast, SnackBar is included in Activity. So, if you press Home button, Toast is still shown, but snackBar is hidden with the activity. Because, snackbar is used for showing message to users and interact with users too.

In SnackBar codes in Android source, it finds the parent view group using view parameter. And Snackbar layout is included in the view group.

sso.techie
  • 136
  • 7
  • i never thought that it can happen this using loginbutton as view , can you explain something on this? – Prashant singh Oct 18 '18 at 07:41
  • First, The first parameter of `SnackBar.make()` function is View type, and `loginButton` is Button type which extends View. So `loginButton` is also type of View. And, concept of SnackBar and is different from toast's. – sso.techie Oct 18 '18 at 08:07